资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#连接SqlServer

  String userName = textBox1.Text;//用户名

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都做网站、禹城网络推广、重庆小程序开发、禹城网络营销、禹城企业策划、禹城品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供禹城建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

            String userPw = textBox2.Text;//密码

        string connString = @"Data Source=ASUS\SQLEXPRESS;Initial Catalog=StudentScore;Integrated Security=True";

            SqlConnection conn = new SqlConnection(connString);

            string sql = String.Format("select count(*) from [user] where UserName='{0}'and Password='{1}'", userName, userPw);

            try

            {

                conn.Open();// 打开数据库连接

                SqlCommand comm = new SqlCommand(sql, conn);//创建 Command 对象

                int n = (int)comm.ExecuteScalar();//执行查询语句,返回匹配的行数  

                if (n==1)

                {

                    Console.WriteLine("----->" + userName + "  " + userPw + "登陆成功");

                    this.DialogResult = DialogResult.OK;

                    this.Tag = true;

                    Form2 f2 = new Form2();

                    f2.Show();

                    this.Hide();

                }

                else

                {

                    MessageBox.Show("用户名或者密码错误", "登录失败",

                       MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    this.Tag = false;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "操作数据库出错!",

                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                this.Tag = false;

            }

            finally

            {

                conn.Close();// 关闭数据库连接

            }


分享文章:C#连接SqlServer
URL标题:http://cdkjz.cn/article/jpcdhd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220