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

}