阅读器关闭时尝试调用read无效

左手堙 2012-09-18 08:54:55

//查询语句中字段为 * 时的通用方法。
private static List<Admin> GetAdminBySql(CommandType commandType, string commandText, params SqlParameter[] paras)
{
List<Admin> list = new List<Admin>();
using (SqlDataReader reader = DBHelper.ExecuteGetReader(commandType, commandText, paras))
{
while (reader.Read())
{
Admin admin = new Admin
{
Id = (int)reader["Id"],
LoginId = reader["LoginId"].ToString(),
LoginPwd = reader["LoginPwd"].ToString(),
Name = reader["Name"].ToString(),
Phone = reader["Phone"].ToString()
};
list.Add(admin);
}
}
return list;
...全文
375 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
左手堙 2012-09-19
  • 打赏
  • 举报
回复
哦哦 搞定了 没写datatable
不过还得谢谢你
孟子E章 2012-09-18
  • 打赏
  • 举报
回复
DataTable x = new DataTable();
x.Load(datareader)

return x;
左手堙 2012-09-18
  • 打赏
  • 举报
回复
嗯 是的 那我该怎么修改?
孟子E章 2012-09-18
  • 打赏
  • 举报
回复
DBHelper里面包连接关掉了或者阅读器关掉都是不行的,建议传回DataTable
  • 打赏
  • 举报
回复
DBHelper.ExecuteGetReader(commandType, commandText, paras)
你这里面把阅读器关掉了吧?
左手堙 2012-09-18
  • 打赏
  • 举报
回复
阅读器关闭时尝试调用 Read 无效。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 阅读器关闭时尝试调用 Read 无效。

源错误:


行 83: using (SqlDataReader reader = DBHelper.ExecuteGetReader(commandType, commandText, paras))
行 84: {
行 85: while (reader.Read())
行 86: {
行 87: Admin admin = new Admin

孟子E章 2012-09-18
  • 打赏
  • 举报
回复
没有你加一个
public DataTable ExecuteGetTable(commandType, commandText, paras)
{
....这里跟ExecuteGetReader里面的代码类似,得到datareader 只是最后
DataTable x = new DataTable();
x.Load(datareader);
return x;
}
左手堙 2012-09-18
  • 打赏
  • 举报
回复
里面有写过dahelper类 helper里面 没写ExecuteGetTable
孟子E章 2012-09-18
  • 打赏
  • 举报
回复
DBHelper你有代码吗?

你可以使用DBHelper的其他方法啊,里面一般不止一个方法啊
左手堙 2012-09-18
  • 打赏
  • 举报
回复
哦哦 真心不懂 初学的多多见谅。麻烦说的清楚点好么 谢谢哈……
孟子E章 2012-09-18
  • 打赏
  • 举报
回复
改你的SqlHepler
或这选择返回DataTable的方法

写了直接报错哦

你写哪了?你用脑子想一想,直接写行吗?类型都不对,

using (DataTable xx = DBHelper.ExecuteGetTable(commandType, commandText, paras)

DBHelper.ExecuteGetTable里面定义返回DataTablle的方法,在ExecuteGetReader里面添加
DataTable x = new DataTable();
x.Load(datareader)

return x;
就能返回DataTable了,你明白了,吗???
左手堙 2012-09-18
  • 打赏
  • 举报
回复
写了直接报错哦

109,900

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧