每次交易插入100条记录

时间:2010-07-22 11:07:20

标签: sql linq-to-sql

每个transactionScope一次只能插入100条记录吗? 我想以这种方式做事,以避免我的申请超时。

using(var scope = new TransactionScope())
{

foreach ( object x in list)

        {
          // doing insertOnSubmit here
        }

context.SubmitChanges();
scope.Complete();

}

所以我想在该事务中只插入100行甚至50行,以避免超时。

2 个答案:

答案 0 :(得分:3)

这样的东西?

TransactionScope scope;
bool committed = false;
int i = 0;
const int batchSize = 100; // or even 50

try
{
    foreach ( object x in list)
    {
        if (i % batchSize == 0)
        {
            if (scope != null)
            {
                scope.Commit();
                scope.Dispose();
                context.SubmitChanges();
            }
            scope = new TransactionScope();
        }

        // doing insertOnSubmit here
        ++i;
    }

    if (scope != null)
    {
        scope.Commit();
        context.SubmitChanges();
    }
}
finally
{
   if (scope != null) 
   {
       scope.Dispose();
   }
}

答案 1 :(得分:1)

不确定。每50-100条记录就开始一次新的交易。你的确切问题是什么?