private void btlogin_Click(object sender, EventArgs e)
{
if (txtname.Text == "" || txtpassword.Text == "")//判断用户名是否为空
{
MessageBox.Show("用户名或密码不能为空", "登录提示");
}
try
{
string name, pass;
bool flag = false;
//获取用户名和密码
name = txtname.Text;
pass = txtpassword.Text;
string Conn = "Data Source=LAPTOP-FHUHF40G;Initial Catalog=SHUJU;Integrated Security=True";
SqlConnection connection = new SqlConnection(Conn);//创建数据库连接类对象
connection.Open();//打开链接
//从数据库中华搜索用户名和密码
string sql = string.Format("SELECT COUNT(*)FROM user info WHERE username = '{0}'AND password = '{1}'", name, pass);
SqlCommand command = new SqlCommand(sql, connection);
int i = Convert.ToInt32(command.ExecuteScalar());
if (i > 0)
{
MessageBox.Show("登录成功!");
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
}
else
{
MessageBox.Show("用户名或者密码错误!");
}
connection.Close();
}
catch
{
MessageBox.Show("出现不可预知的错误!");
}
}