2018.09.16 命令参数的使用

private void btn_Login_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=EduBaseDemo;Integrated Security=sspi";
SqlCommand sqlCommand = sqlConnection.CreateCommand();

sqlCommand.CommandText =

"SELECT COUNT(1) FROM tb_User WHERE No=@No AND Password=HASHBYTES('MD5',@Password);")
SqlParameter sqlParameter = new SqlParameter(); 
sqlParameter.ParameterName = "@No"; 
sqlParameter.Value = this.UserNo.Text.Trim(); 
sqlParameter.SqlDbType = SqlDbType.Char; 
sqlParameter.Size = 10; 
sqlCommand.Parameters.Add(sqlParameter); 
sqlConnection.Open();
int rowCount = (int)sqlCommand.ExecuteScalar(); 
sqlConnection.Close(); 
if (rowCount == 1) 
{
MessageBox.Show("登录成功。"); 
}
else 
{
MessageBox.Show("用户号/密码有误,请重新输入!"); 
this.Password.Focus(); 
this.Password.SelectAll(); 
}
}

猜你喜欢

转载自www.cnblogs.com/Holiday-L/p/9658011.html