![]() |
#2
Maick2015-04-16 10:11
|
private void button1_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "" || txtUserPassword.Text.Trim == "")
{
MessageBox.Show("用户名或密码不能为空!");
txtUserName.Focus();
return;
}
string connString = @"Data Source=windows-PC;Initial Catalog=AddressList;Integrated Security=True";
string sqlStr = string.Format("select count(*) from [User] where UserName='{0}' and Password='{1}'",txtUserName.Text.Trim,txtUserPassword.Text.Trim());
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand amd = new SqlCommand(sqlStr, conn);
conn.Open();
int n=Convert.ToInt32(cmd.ExecuteScalar());
if (n == 1)
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("用户名或密码错误!","错误");
txtUserName.Text = "";
txtUserPassword.Text = "";
txtUserName.Focus();
}