如何将大量数据插入数据库[重复]

时间:2022-11-27 13:48:55

This question already has an answer here:

这个问题在这里已有答案:

I have a programm in C# which downloads data from 4 sources (2 excel sheets,oracle and access database) and calculate them against each other. In the result I have some big results, I keep the results in List. Amount of rows in the result equals approximately 120.000. One row is about 10MB. The result inserts into access database.

我在C#中有一个程序,它从4个来源(2个excel表,oracle和访问数据库)下载数据并相互计算。在结果中我有一些很大的结果,我将结果保存在List中。结果中的行数大约等于120.000。一行大约10MB。结果将插入到访问数据库中。

How do I insert this list into my databse? Can someone give me an example?

如何将此列表插入我的数据库?有人能举个例子吗?

Now I insert rows into table one by one. It costs me about 3 hours to do that.

现在我逐行将行插入表中。这花了我大约3个小时。

1 个解决方案

#1


2  

Use some kind of bulk insert. If your use entity framework to work with your db's something like this is what you need. Max out the timeout as well.

使用某种大容量插入物。如果你的使用实体框架与你的db这样的东西是你需要的。最大限度地延长超时。

 using (var transactionScope = new System.Transactions.TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 10, 0)))
            {
                try
                {
                    ctx.BulkInsert(productsToSync, new BulkInsertOptions()
                    {
                        TimeOut = 10 * 60 * 1000
                    });
                    await ctx.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    SystemLogManager.AddDataSyncErrorLog(ex);
                }
                finally
                {
                    transactionScope.Complete();
                }
            }

#1


2  

Use some kind of bulk insert. If your use entity framework to work with your db's something like this is what you need. Max out the timeout as well.

使用某种大容量插入物。如果你的使用实体框架与你的db这样的东西是你需要的。最大限度地延长超时。

 using (var transactionScope = new System.Transactions.TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 10, 0)))
            {
                try
                {
                    ctx.BulkInsert(productsToSync, new BulkInsertOptions()
                    {
                        TimeOut = 10 * 60 * 1000
                    });
                    await ctx.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    SystemLogManager.AddDataSyncErrorLog(ex);
                }
                finally
                {
                    transactionScope.Complete();
                }
            }