private void btnLogin_Click(object sender, EventArgs e)
{
int count = 0;
// 验证
string uid = txtUid.Text.Trim();
string pwd = txtPwd.Text;
if (uid.Length == 0 || pwd.Length == 0) { return; }
string connStr = @"server=.\sqlexpress;database=HeiMa8;uid=sa;pwd=123;";
// 1改写SQL语句
string sql = "select count(*) from Exe2.loginTbl where uid=@uid and pwd=@pwd;";
// 2绑定参数与值
SqlParameter[] paras = {
new SqlParameter("@uid", uid),
new SqlParameter("@pwd", pwd)
};
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// 3将参数交给执行对象
cmd.Parameters.AddRange(paras);
conn.Open();
count = (int)cmd.ExecuteScalar();
}
}
if (count > 0)
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("登录失败");
}
}
{
int count = 0;
// 验证
string uid = txtUid.Text.Trim();
string pwd = txtPwd.Text;
if (uid.Length == 0 || pwd.Length == 0) { return; }
string connStr = @"server=.\sqlexpress;database=HeiMa8;uid=sa;pwd=123;";
// 1改写SQL语句
string sql = "select count(*) from Exe2.loginTbl where uid=@uid and pwd=@pwd;";
// 2绑定参数与值
SqlParameter[] paras = {
new SqlParameter("@uid", uid),
new SqlParameter("@pwd", pwd)
};
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// 3将参数交给执行对象
cmd.Parameters.AddRange(paras);
conn.Open();
count = (int)cmd.ExecuteScalar();
}
}
if (count > 0)
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("登录失败");
}
}