【发布时间】:2012-01-21 10:38:08
【问题描述】:
我遇到了一个错误,提示我的数据读取器已打开。
我的代码是这样的
public static Users GetByID(int ID, SqlConnection connection)
{
SqlCommand command = new SqlCommand("Select Name, Email, LastLogin, FK_Role_ID from Users where ID=@id");
command.Connection = connection;
command.Parameters.Add(new SqlParameter("id", ID));
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Users user = new Users();
user.ID = ID;
user.Name = reader.GetString(0);
user.Email = reader.GetString(1);
user.LastLogin = reader.GetString(2);
user.role = Role.GetRoleByID(reader.GetInt32(3), connection);
reader.Close();
return user;
}
else
{
reader.Close();
return null;
}
}
Role.GetRoleByID 中出现错误,表明 datareader 命令已打开。这是真的,但是我如何使用我的读者提供的信息调用 Role.GetRoleByID。
我用 c# 和 ASP.NET 编写代码
【问题讨论】:
-
为什么不在查询中加入 GetRoleByID?
标签: c# asp.net .net datareader