简体   繁体   English

将DataTable引入SQL Server的最快方法是什么?

[英]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.

相关问题 从T-SQL数据表中获取单词的最快方法是什么? - What is a fastest way to get words from T-SQL datatable? 有条件地更新DataTable中数据的最快方法是什么? - What is the fastest way to conditionally update data in a DataTable? 在SQL Server中选择整个表的最快方法是什么? - What is the Fastest Way to Select a Whole Table in SQL Server? 在SQL Server CE Winforms中查找数据的最快方法是什么 - What is fastest way to find data in SQL Server CE Winforms 将大量记录插入SQL Server数据库的最快方法是什么? - What is the fastest way to insert a large amount of records into a SQL Server DB? 使用C#在SQL Server中保存数据的最快方法是什么? - What is the fastest way to save data in SQL Server using C#? 从C#在SQL Server中插入记录的最快方法是什么 - What is the fastest way to insert record in SQL Server from C# 将 dataGridView 行导出到 Excel 或 SQL 服务器数据库的最快方法是什么 - What is the fastest way to export dataGridView rows to Excel or into an SQL Server database 更新数据表的最快方法 - Fastest way to update datatable 将 C# 中的数据表导出到 MS Excel 的最快方法是什么? - What is the fastest way to export a DataTable in C# to MS Excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM