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(); // 關閉數據庫鏈接
}