我的代码如下:
protected SqlDataReader ExecuteReader(string cmdText, CommandType cmdType)
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(cmdText, cn);
cmd.CommandType = cmdType;
cn.Open();
return cmd.ExecuteReader( CommandBehavior.CloseConnection);
}
} public List<Category> getShopCategoryList()
{
string command = "dbo.GetCategoryList";
List<ShopCategoryDetails> list = new List<Category>();
SqlDataReader reader = ExecuteReader(command, CommandType.StoredProcedure);
while (reader.Read())
{
list.Add(new ShopCategoryDetails((int)reader[0],reader[1].ToString()));
}
return list;
}在读到while (reader.Read())这句时,就给报错:阅读器关闭时尝试调用 Read 无效。 异常详细信息: System.InvalidOperationException: 阅读器关闭时尝试调用 Read 无效。请问这咋整啊
protected SqlDataReader ExecuteReader(string cmdText, CommandType cmdType)
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(cmdText, cn);
cmd.CommandType = cmdType;
cn.Open();
return cmd.ExecuteReader( CommandBehavior.CloseConnection);
}
} public List<Category> getShopCategoryList()
{
string command = "dbo.GetCategoryList";
List<ShopCategoryDetails> list = new List<Category>();
SqlDataReader reader = ExecuteReader(command, CommandType.StoredProcedure);
while (reader.Read())
{
list.Add(new ShopCategoryDetails((int)reader[0],reader[1].ToString()));
}
return list;
}在读到while (reader.Read())这句时,就给报错:阅读器关闭时尝试调用 Read 无效。 异常详细信息: System.InvalidOperationException: 阅读器关闭时尝试调用 Read 无效。请问这咋整啊
解决方案 »
- TextBox下拉记录
- vs2008 添加ajax控件提示 命名空间“System.Web.UI”中不存在类型或命名空间名称“UpdatePanel”(是缺少程序集引用吗?)
- 菜鸟,高手们都进来
- “/”应用程序中的服务器错误。未将对象引用设置到对象的实例。
- ASP.Net 中MVC 模式 与三层结构有何区别...
- while 双从循环判断问题 急
- asp.net和c#有什么区别
- 求救!!!关于TreeView控件的一个问题!
- 谁能给给看一下啊”请看问题?
- 哪有visio中文版下载?
- 怎么在保证当前页面不关的情况下跳出另外的指定页面?
- 用ASP.net开发网站,如何使客户机用IE游览时,IE窗口的"关闭"按钮不可用?
{
SqlCommand cmd = new SqlCommand(cmdText, cn);
cmd.CommandType = cmdType;
cn.Open();
return cmd.ExecuteReader( CommandBehavior.CloseConnection);
} //在这里connection就已经close了,因为你使用了use你可以试着讲SqlConnection cn 变成类成员变量。但是要记得close它。
不要使用using应该可以