事务范围中的表适配器更新可以保持超时

时间:2011-05-18 18:31:30

标签: c# .net-3.5 transactions strongly-typed-dataset

所以我有一个类型化的数据集,我已经从另一个数据库创建了记录(超过500,000条记录!)。我需要将所有这些记录导入到其他数据库中并使用以下代码(减去初始化,添加行等):

try
{
    Console.WriteLine("Time to process of adding to table: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());

    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(1, 0, 0)))
    {
        laborTicketTableAdapter.Update(laborTicket);
        ts.Complete();
    }
    Console.WriteLine("Time to process transaction: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());
}
catch (SqlException ex)
{
    MessageBox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
    result = false;
}
catch (Exception ex)
{
    MessageBox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
    result = false;
}

控制台输出以下内容:

Time to process of adding to table: Start at 1/1/2009 2:05:59 PM | End at 5/18/2011 2:06:30 PM
The thread '<No Name>' (0xa5c) has exited with code 0 (0x0).
The thread '<No Name>' (0x2e4) has exited with code 0 (0x0).
The thread '<No Name>' (0xae0) has exited with code 0 (0x0).
A first chance exception of type 'System.Transactions.TransactionAbortedException' occurred in System.Transactions.dll

它抛出异常,内部异常为“Transaction timeout”。交易已经过期,因此我将超时设置为一小时,并且在15分钟后仍然超时。发生了什么事?

处理的最后时间是下午2:19:40,大约需要15分钟才能运行。为什么我得到这个例外?我在控制台上看到了三个线程但是在一个空的项目中这是一个空白的平板。所有其他连接都关闭到外部数据源,我执行更新时剩下的就是表适配器和表对象。

- 编辑 - Machine.config没有maxTimeout属性的任何设置

1 个答案:

答案 0 :(得分:0)

异常可能有各种原因。也许您的实际SQL连接有超时?当你的表需要重新组织时(这里只是推测)

,这可能会发生

作为sidenode,你看过SqlBulkCopy了吗? (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx) - &gt;&gt;允许您使用来自其他来源的数据有效地批量加载SQL Server表。

根据我的经验,这非常快。