简体   繁体   English

将记录批量插入SQL Server数据库

[英]Bulk insert records into SQL Server database

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#? 从C#将它们插入SQL Server数据库的最佳方法是什么?

Can I use BULK INSERT ? 我可以使用BULK INSERT吗?

Best way is to use the bcp utility or an SSIS workflow . 最好的方法是使用bcp实用程序SSIS工作流程 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. 下一个最佳选择是BULK INSERT语句,只要SQL Server引擎本身可以访问该文件即可。 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. 最后一个选择是SqlBulkCopy类,该类允许您的应用读取文件,也许对其进行处理和转换,然后将数据作为枚举数提供给SqlBulkCopy。

I recently worked on same kind of problem , i realized there are couple solutions to it. 我最近在处理同样的问题,意识到有几种解决方案。

I wrote a BatchProgram (for batch program design read this - http://msdn.microsoft.com/en-us/magazine/cc164014.aspx ) 我写了一个BatchProgram(对于批处理程序设计,请阅读此书-http: //msdn.microsoft.com/zh-cn/magazine/cc164014.aspx

You can use SQL Server Utilities either BCP.exe or OSQL.exe or .net framework supplied SQLBulkCopy class. 您可以使用SQL Server实用程序BCP.exe或OSQL.exe或.net框架提供的SQLBulkCopy类。

I ended up using BCP ( i got a CSV file and used a formatting file and load the data) and OSQL ( i used OSQL where i have to supply a file to the the stored proc ) 我最终使用了BCP(我得到了CSV文件,并使用了格式化文件并加载了数据)和OSQL(我使用了OSQL,必须将文件提供给存储的proc)

i also went to .NET Process class and used outputdatarecieved event to log all output of BCP.exe into console (Read this - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx ) this worked pretty well. 我还去了.NET Process类,并使用outputdatarecieved事件将BCP.exe的所有输出记录到控制台中(请阅读此文章-http: //msdn.microsoft.com/zh-cn/library/system.diagnostics.process.outputdatareceived。 aspx )效果很好。

I also tried to SQLBulkCopy class , but it can be slow if you load data first to datatable ( http://msdn.microsoft.com/en-us/library/ex21zs8x.aspx ) , if you use IDataReader ( http://msdn.microsoft.com/en-us/library/434atets.aspx ) it could be fast . 我也尝试了SQLBulkCopy类,但是如果您使用IDataReader( http:// ),则如果先将数据加载到数据表( http://msdn.microsoft.com/en-us/library/ex21zs8x.aspx )可能会很慢。 msdn.microsoft.com/en-us/library/434atets.aspx ),它可能很快。

Since i had millions rows i tried using CSVReader ( http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader ) which is pretty fast .But down the line there was too much problem in data conversion and i did not have much flexibity in SQL server side . 由于我有数百万行,因此我尝试使用CSVReader( http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader ),该速度非常快。但是在数据转换和转换方面存在太多问题我在SQL Server方面没有太多灵活性。

I ended up using BCP and OSQL. 我最终使用了BCP和OSQL。

Is using C# a requirement? 是否需要使用C#? The fastest way is to use the bcp command line tool: http://msdn.microsoft.com/en-us/library/ms162802.aspx 最快的方法是使用bcp命令行工具: http : //msdn.microsoft.com/zh-cn/library/ms162802.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM