我在这段代码中使用了TransactionScope:
private void ExecuteSP()
{
bool IsComplete = false;
SqlCommand sqlComm = null;
//6 hours!!!
TimeSpan ts1 = new TimeSpan(6, 0, 0);
try
{
using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew, ts1))
{
using (SqlConnection sqlConn = new SqlConnection(GetConnectionString()))
{
//open sql connection
sqlConn.Open();
try
{
//create new sqlCommand
sqlComm = new SqlCommand();
for (int i = 1; i <= 2; i++)
{
IsComplete = true;
//This command takes 15 minutes
sqlComm.CommandText = "exec TestSp";
sqlComm.Connection = sqlConn;
sqlComm.CommandType = CommandType.Text;
sqlComm.CommandTimeout = 18000;
//Executing my command
int j = sqlComm.ExecuteNonQuery();
}
//End
t.Complete();
}
catch (Exception ex)
{
IsComplete = false;
string Message = ex.Message;
}
finally
{
if (sqlComm != null)
sqlComm.Dispose();
}
}
}
}
catch (Exception ex)
{
string messagee = ex.Message;
//do something
}
finally
{
MessageBox.Show("Finsh");
}
}
当执行sqlCommand.ExecuteNonQuery()超过10分钟时,会出现这种情况。在此点上,我没有收到任何异常,但在下一次执行时,我会收到以下异常:
The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements.
这是因为System.Transactions.TransactionManager.MaximumTimeout被设置为10分钟的TimeSpan。
我搜索并发现可能与“System.Transactions-->machine.config的maxTimeout”有关,但在更改配置文件后,我收到了一个异常:
<?xml version="1.0"?>
<configuration>
<system.transactions>
<machineSettings maxTimeout="10:00:00"/>
</system.transactions>
<appSettings>
<add key="FileName" value="MyFileName" />
<add key="MySpace" value="5 MB" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
</configuration>
当我在运行时更改配置文件后尝试获取System.Transactions.TransactionManager.MaximumTimeout时,我遇到了以下异常:
有人知道如何解决这个问题吗?(关于我的情况的一般说明:我需要执行存储过程,该存储过程需要约20分钟,因为我需要将包含int的表转换为SQL中的bigint(int = 32位,bigint =64位)。我需要创建新表并将旧表中的数据插入到具有int64的新表中。该表通过ID连接到其他4个表,每个表都包含超过2000万行,并且还有绑定、索引等。我无法将此过程拆分为小型存储过程,因此我需要将最大超时时间更改为一小时或更长时间,10分钟不够!)"配置系统初始化失败"