【问题标题】:SqlDataReader and concurrency/deallocation issuesSqlDataReader 和并发/释放问题
【发布时间】: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


【解决方案1】:

锁需要在阅读器级别...多个线程正在打开阅读器

【讨论】:

  • @Totero 我很高兴有人有礼貌地发表评论。谢谢。来自op的可怜的东西。垃圾邮件商人。
  • 哦,你喜欢他卖的东西!它显然把男孩们带到了院子里
猜你喜欢
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
相关资源
最近更新 更多