C#链接SqlServer

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

            String userPw = textBox2.Text;//密码数据库

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

            SqlConnection conn = new SqlConnection(connString);this

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

            tryorm

            {对象

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

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

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

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

            }

相关文章
相关标签/搜索