2016-06-28 66 views
0

我有一個ASP.NET應用程序需要從Excel文件加載數據。 該文件包含約20K條記錄。應用程序從文件中讀取數據並循環遍歷每條記錄,進行計算和驗證,然後將每條記錄插入到數據庫中。一切都按預期工作,直到Insert方法拋出異常。運行10 - 11分鐘後出現錯誤。 注:所有加載過程運行到是按以下方式定義的事務範圍:無效嘗試調用讀取器關閉時讀取錯誤當調用ExecuteScalar方法時出錯

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

所有時間的SQLConnection打開 - 我必須確保這一使用SQL事件探查器。 爲了使用數據庫,我們使用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) 

我搜索這個錯誤,並且主要思想是連接被關閉的信息,但我想不通爲什麼?

我將不勝感激任何幫助或建議

+0

添加'Debug.Print(myConn.State)'。你會發現連接已關閉。現在追溯。這段代碼很可能是無辜的。 – usr

+0

嗨!我明白,在拋出異常的情況下,連接已關閉。我需要了解關閉的原因。謝謝 –

回答

0

好吧,我已經找到了問題的根源。這是一個交易超時。 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

相關問題