最近在练习一个ADO.NET三层架构验证用户名登录密码时出现报错:“阅读器关闭时尝试调用 HasRows 无效”
百度了好多文章才得到解决:
先来看SqlHelper获取SqlDataReader
public static SqlDataReader ExecuteDataReader(string sql, params SqlParameter[] Parameters)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
foreach (SqlParameter p in Parameters)
{
cmd.Parameters.Add(p);
}
try
{
Connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
在来看获取SqlDataReader代码:
public UserModel GetUserLoginInfoByLoginId(string LoginId)
{
UserModel model = null;
string sql = "select loginid,password from hrmresource where loginid=@Uid";
SqlDataReader reader = SqlHelper.ExecuteDat