【发布时间】: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