数据读取器已打开
我遇到错误,提示我的数据读取器已打开。
我的代码如下所示
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 编写代码
I got a problem with an error that say that my datareader is already open.
My code looks like this
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;
}
}
The error occours in the Role.GetRoleByID saying that the datareader command is alreader open. Which is true, but how do I call Role.GetRoleByID with the information from my reader.
I code in c# and ASP.NET
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您的
Role.GetRoleByID
将尝试重用该连接。选项:
GetByID
内的SqlDataReader
获取所需的数据,关闭该读取器,然后然后调用Role.GetRoleByID
(所以你一次只有一个活跃的读者)如果我是你,我会选择第一个选择——或者可能是最后一个。我还会使用
using
语句自动关闭阅读器:作为第四个选项 - 为什么不在现有查询中执行
GetRoleByID
所需的联接?这意味着您只需要访问一次数据库。It looks like your
Role.GetRoleByID
will try to reuse the connection.Options:
SqlDataReader
withinGetByID
, close that reader, and then callRole.GetRoleByID
(so you've only got one active reader at a time)I'd go with the first option if I were you - or possibly the last. I'd also use a
using
statement to close the reader automatically:As a fourth option - why not just perform the join required by
GetRoleByID
in your existing query? That would mean you'd only need one trip to the database.您可以考虑使用具有必要联接的选择查询,以便能够从同一查询接收角色。
另外,建议使用
(using reader = command.ExecuteReader() )
,以便在范围结束后立即关闭读取器并进行处理。You may consider using a Select query with requisite join, to be able to receive Role from the same query.
Also, it is recommended to use
(using reader = command.ExecuteReader() )
, so that the reader is closed and Disposed as soon as the scope is over.您是否允许 MARS 连接字符串 (
MultipleActiveResultSets=true
)?Have you allowed MARS your connection string (
MultipleActiveResultSets=true
)?