【发布时间】:2012-05-23 16:29:40
【问题描述】:
运行下面的代码给我一个错误:
.NET Framework 执行因内存不足而被升级策略中止。 System.InvalidOperationException: 已经有一个打开的 DataReader 与此命令关联,必须先关闭。
我到处都有以下代码来运行我的 sql:
sql.Append(string.Format("SELECT TableId FROM ps_SavedTables WHERE guid = '{0}'", guid));
using (IDataReader reader = SqlHelper.GetDataReader(sql.ToString())) {
if (reader.Read()) {
result = reader.IsDBNull(0) ? string.Empty : reader[0].ToString();
}
//CDW added to close SqlDataReader
reader.Close();
}
GetDataReader 是这样定义的:
public static SqlDataReader GetDataReader(string sql, string connectionString) {
lock (_lock) {
SqlConnection connection = null;
try {
connection = GetConnection(connectionString);
//connection.Open();
using (SqlCommand cmd = new SqlCommand(sql, connection)) {
WriteDebugInfo("GetDataReader", sql);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
catch (Exception e) {
if (connection != null)
connection.Dispose();
throw new DataException(sql, connectionString, e);
}
}
}
【问题讨论】:
-
在哪一行抛出异常?
-
你能告诉我们更多关于异常的细节吗?扔到哪里去了?堆栈跟踪等?
标签: c# asp.net .net sql-server-2005 sqldatareader