C#连接SqlServer

发布时间:2020-06-16 22:27:41 作者:ZWOOM
来源:网络 阅读:400

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

            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(); // 关闭数据库连接

            }


推荐阅读:
  1. php pdo连接sqlserver配置
  2. SqlServer系列笔记——连接查询JION

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

数据库 连接

上一篇:intel A20地址线

下一篇:powershell新建数据库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》