当读取器关闭时试图调用Read无效,调用ExecuteScalar方法时出现错误

本文关键字:调用 ExecuteScalar 方法 错误 无效 Read 读取 | 更新日期: 2023-09-27 17:53:02

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

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

一直打开SQLConnection -我使用SQL Profiler确保这一点。为了与数据库一起工作,我们使用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)

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

我将非常感谢任何帮助或建议

当读取器关闭时试图调用Read无效,调用ExecuteScalar方法时出现错误

好的,我找到了问题的根源。这是一个事务超时。系统。事务有MaxTimeout属性,它的默认值是10分钟。使用代码或应用/web配置,你只能减少它的价值。为了增加它,您必须在机器中配置它。通过在文件的配置部分的END中添加以下块:

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

以下是一些相关文章:覆盖系统。代码中的事务默认超时为10分钟。

maxTimeout从机器的值。