批量插入记录到SQL Server数据库批量、数据库、SQL、Server

2023-09-06 18:28:01 作者:香烟如寂寞

我有一个包含约一百万条记录的文本文件。是什么将它们插入到从C#?

I have a text file that contains about a million records. What is the best way to insert them into a SQL Server database from C#?

我可以使用 BULK INSERT

推荐答案

最好的办法是使用 BCP 实用或 SSIS的工作流。这些工具有像缓存改进和配料你会在一个天真的实施错过。接下来最好的选择是 BULK INSERT 声明只要SQL Server引擎本身可达到该文件。最后的选择将是SqlBulkCopy级,从而使您的应用程序做阅读文件,可能处理它,改造它,然后喂数据作为枚举器使用SqlBulkCopy。

Best way is to use the bcp utility or an SSIS workflow. Those tools have refinements like caching and batching your will miss in a naive implementation. Next best option is BULK INSERT statement, as long as the SQL Server engine itself can reach the file. Last option would be SqlBulkCopy class which allows your app do read the file, maybe process it and transform it, then feed the data as an enumerator to the SqlBulkCopy.