急问题There is already an open DataReader associated with this Command which must b

zy_1985 2009-07-16 11:16:55
做了一个小网站,有时候会出这个问题,知道应该是个常见的问题,但也不太清楚如何解决,请高手指点啊!
There is already an open DataReader associated with this Command which must be closed first.

调用底层代码:(还请高手指点不足之处,先谢谢了!)
public static DataTable GetDataTable(string contype, string sql, string tname)
{
DataTable dt = new DataTable();
using (SqlConnection conn = GetConnection(contype))
{
SqlCommand comm = new SqlCommand();
try
{
comm.Connection = conn;
comm.CommandText = sql;
comm.CommandTimeout = 100;
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
da.Fill(dt);
}
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
comm.Dispose();
conn.Close();
conn.Dispose();
}
}
return dt;
}
/// <summary>
/// 通过 SqlParameter[] 得到表
/// </summary>
/// <param name="contype">数据库连接的名字</param>
/// <param name="procName">存储过程名称</param>
/// <param name="tname">表名</param>
/// <param name="paras">存储过程的值</param>
/// <returns>表</returns>
public static DataTable GetDataTable(string contype, string procName, string tname, SqlParameter[] paras)
{
using (SqlConnection conn = GetConnection(contype))
{
// SqlDataAdapter da = null;
DataTable dt = new DataTable();
SqlCommand comm = new SqlCommand();
try
{
comm.Connection = conn;
comm.CommandText = procName;
comm.CommandTimeout = 100;
if (paras != null)
{
comm.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter para in paras)
{
comm.Parameters.Add(para);
}
}
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
da.Fill(dt);
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
finally
{
comm.Dispose();
conn.Close();
conn.Dispose();
}
return dt;
}
}
...全文
3870 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
IHandler 2009-07-16
  • 打赏
  • 举报
回复
web.config中
<customErrors mode="Off" >

然后运行页面,看页面中下部的提示
zy_1985 2009-07-16
  • 打赏
  • 举报
回复
这个就是网站的异常信息,其它没什么了,搞得我郁闷
There is already an open DataReader associated with this Command which must be closed first.
IHandler 2009-07-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zy_1985 的回复:]
就是这个Catch抓住的异常,高手在帮忙看看吧
    /// <summary>
    /// 活动快报前5条记录
    /// </summary>
[/Quote]
这事程序代码,哪是异常信息呢?
宝_爸 2009-07-16
  • 打赏
  • 举报
回复
而且看你的command和connection都是新创建的,好像也没啥问题啊。
宝_爸 2009-07-16
  • 打赏
  • 举报
回复
还是没有DataReader 啊
zy_1985 2009-07-16
  • 打赏
  • 举报
回复
就是这个Catch抓住的异常,高手在帮忙看看吧
/// <summary>
/// 活动快报前5条记录
/// </summary>
/// <returns></returns>
public string selectReport()
{
int news_id = 0;//新闻ID
string news_title = string.Empty;//新闻标题

StringBuilder sbr = new StringBuilder();
DataTable dt = new DataTable();
string sql = "select top 13 id,title from News where BigClassName='活动快报' order by red desc,infotime desc";
//string sql = "select top 5 id,title from News order by infotime desc";
try
{
sbr.Append("<ul>\n");
dt = DBUtility.DataBase.GetDataTable("one", sql, "News");
int i = 0;
foreach (DataRow row in dt.Rows)
{
news_id = Convert.ToInt32(row["id"].ToString());
news_title = row["title"].ToString();
if (i == 0)
{
sbr.Append("<li class=\"FONTsize\"><a href=\"/2009/News.aspx?id=" + news_id + "\"><B>" + news_title + "</B></a></li>\n");
}
else
{
if (i % 2 == 0)
{
sbr.Append("<td width=\"50%\">\n");
sbr.Append("<a href=\"/2009/News.aspx?id=" + news_id + "\" title=\"" + news_title + "\">" + ExaminesList.GetString(news_title, 30) + "</a>\n");
sbr.Append("</td>\n</tr>\n</table>\n");
sbr.Append("</li>\n");
}
else
{
sbr.Append("<li>\n");
sbr.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"396\">\n<tr>\n<td width=\"50%\">\n");
sbr.Append("<a href=\"/2009/News.aspx?id=" + news_id + "\" title=\"" + news_title + "\">" + ExaminesList.GetString(news_title, 30) + "</a>\n");
sbr.Append("</td>\n");
}
}
i++;
}
if (i % 2 == 0)
{
sbr.Append("<td width=\"50%\">\n");
sbr.Append("</td>\n</tr>\n</table>\n");
sbr.Append("</li>\n");
}
sbr.Append("</ul>\n");
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
if (dt != null)
{
dt.Dispose();
}
}
return sbr.ToString();
}
IHandler 2009-07-16
  • 打赏
  • 举报
回复
看看堆栈跟踪的信息
PS
断点调试一下
zzs_happy 2009-07-16
  • 打赏
  • 举报
回复
问题不出在这个页面里。
zy_1985 2009-07-16
  • 打赏
  • 举报
回复
关键的问题就是在这了,我代码里真的没有用到过DataReader啊!
ljhcy99 2009-07-16
  • 打赏
  • 举报
回复
我感觉问题不在你这俩函数里ba ?
应该是DAtaReader用完了但是没有关闭的问题,
wjq 2009-07-16
  • 打赏
  • 举报
回复
找你代码里有没有用DataReader来取数据的,看看那里用完有没有close connection
chen_ya_ping 2009-07-16
  • 打赏
  • 举报
回复
那执行上面代码是先判断一下conn是不是打开是打开的就先关闭,然后再打开。就像这样:
SqlConnection conn = new SqlConnection();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
试试

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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