[英]What is the fastest way to get a DataTable into SQL Server?
I have a DataTable in memory that I need to dump straight into a SQL Server temp table. 我在内存中有一个DataTable,我需要直接转储到SQL Server临时表中。
After the data has been inserted, I transform it a little bit, and then insert a subset of those records into a permanent table. 插入数据后,我将其转换一点,然后将这些记录的子集插入永久表中。
The most time consuming part of this operation is getting the data into the temp table. 此操作中最耗时的部分是将数据导入临时表。
Now, I have to use temp tables, because more than one copy of this app is running at once, and I need a layer of isolation until the actual insert into the permanent table happens. 现在,我必须使用临时表,因为这个应用程序的多个副本一次运行,我需要一层隔离,直到实际插入到永久表中。
What is the fastest way to do a bulk insert from a C# DataTable into a SQL Temp Table? 从C#DataTable批量插入到SQL临时表的最快方法是什么?
I can't use any 3rd party tools for this, since I am transforming the data in memory. 我不能使用任何第三方工具,因为我正在转换内存中的数据。
My current method is to create a parameterized SqlCommand: 我目前的方法是创建一个参数化的SqlCommand:
INSERT INTO #table (col1, col2, ... col200) VALUES (@col1, @col2, ... @col200)
and then for each row, clear and set the parameters and execute. 然后为每一行清除并设置参数并执行。
There has to be a more efficient way. 必须有一种更有效的方式。 I'm able to read and write the records on disk in a matter of seconds... 我能够在几秒钟内读取和写入磁盘上的记录......
您应该使用SqlBulkCopy
类 。
SqlBulkCopy will get the data in very fast. SqlBulkCopy将以非常快的速度获取数据。
I blogged not that long ago how to maximise performance. 我不久前在博客上写了如何最大限度地提高性能。 Some stats and examples in there. 那里有一些统计数据和例子。 I compared 2 techniques, 1 using an SqlDataAdapter and 1 using SqlBulkCopy - bottom line was for bulk inserting 100K records, the data adapter approach took ~25 seconds compared to only ~0.8s for SqlBulkCopy. 我比较了两种技术,1使用SqlDataAdapter,1使用SqlBulkCopy - 底线是批量插入100K记录,数据适配器方法花费约25秒,相比之下,SqlBulkCopy只有~0.8s。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.