【问题标题】:Invalid attempt to call Read when reader is closed error when call ExecuteScalar method调用 ExecuteScalar 方法时读取器关闭错误时尝试调用读取无效
【发布时间】:2016-06-28 08:19:08
【问题描述】:

我有一个 ASP.NET 应用程序需要从 excel 文件加载数据。 该文件包含大约 20K 条记录。该应用程序从文件中读取数据并遍历每条记录,进行计算和验证,然后将每条记录插入数据库。一切都按预期工作,直到 Insert 方法抛出异常。运行 10 - 11 分钟后抛出错误。 注意:所有加载过程都运行在一个按以下方式定义的事务范围内:

 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))

始终打开 SQLConnection - 我已使用 SQL Profiler 确保这一点。 为了使用 DB,我们使用 Microsoft.Practices.EnterpriseLibrary.Data.Database 对象。 这是一个插入方法:

public bool InsertInspectionRide(InspectionRideBE be)
    {
        bool result = false;
        try
        {
            using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("InsertInspectionRide",
                be.param1, be.param2))
            {

                cmd.CommandTimeout = 60000000;


                be.IdRide = Convert.ToInt32(db.ExecuteScalar(cmd));

                result = be.IdRide > 0;
            }
        }
        catch (Exception ex)
        {
            if (ExceptionPolicy.HandleException(ex, "DAL"))
            {
                throw;
            }
        }
        return result;
    }

这是一个错误:

    06/28/2016 10:27:14
Type : System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : 
Source : 
Help link : 
Data : System.Collections.ListDictionaryInternal
TargetSite : 
Stack Trace : The stack trace is unavailable.
Additional Info:

MachineName : XXX
TimeStamp : 6/28/2016 7:27:14 AM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
AppDomainName : /XXX-1-131115702788886173
ThreadIdentity : XXX
WindowsIdentity : XXXX
    Inner Exception
    ---------------
    Type : System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Message : Invalid attempt to call Read when reader is closed.
    Source : System.Data
    Help link : 
    Data : System.Collections.ListDictionaryInternal
    TargetSite : Boolean ReadInternal(Boolean)
    Stack Trace :    at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
       at System.Data.SqlClient.SqlDataReader.Read()
       at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue)
       at System.Data.SqlClient.SqlCommand.ExecuteScalar()
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteScalar(IDbCommand command)
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteScalar(DbCommand command)
       at EnforcementService.DataAccess.InspectionRideDAL.InsertInspectionRide(InspectionRideBE be)

我有关于这个错误的搜索信息,主要想法是连接已关闭,但我不知道为什么?

我将不胜感激任何帮助或建议

【问题讨论】:

  • 添加Debug.Print(myConn.State)。您会发现连接已关闭。现在向后追溯。这段代码可能是无辜的。
  • 嗨!我知道在抛出异常的那一刻连接已关闭。我需要了解关闭的原因。谢谢

标签: c# asp.net database transactionscope executescalar


【解决方案1】:

好的,我找到了问题的根源。这是事务超时。 System.Transaction 具有 MaxTimeout 属性,默认值为 10 分钟。使用代码或应用程序/网络配置,您只能降低它的价值。为了增加它,您必须在 machine.config 文件中配置它,方法是在文件的配置部分的 END 中添加以下块。

<system.transactions>
 <machineSettings maxTimeout="01:00:00" />
</system.transactions>
</configuration> 

以下是一些相关文章: Override the System.Transactions default timeout of 10 minutes in the code.

maxTimeout value from Machine.Config is not picked up by C# winform app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多