如何使用 ADO.Net 最好地插入 350,000 行

发布于 2024-07-16 14:29:59 字数 293 浏览 2 评论 0原文

我有一个包含 350,000 行的 csv 文件,每行大约有 150 列。

使用 ADO.Net 将这些行插入 SQL Server 的最佳方法是什么?

我通常的做法是手动创建 SQL 语句。 我想知道是否有任何方法可以对其进行编码以简单地将整个数据表插入到 SQL Server 中? 或者像这样的一些捷径。

顺便说一句,我已经尝试使用 SSIS 执行此操作,但是有一些数据清理问题,我可以使用 C# 处理,但使用 SSIS 则不太容易。 数据最初为 XML,但为了简单起见,我将其更改为 CSV。

I have a csv file with 350,000 rows, each row has about 150 columns.

What would be the best way to insert these rows into SQL Server using ADO.Net?

The way I've usually done it is to create the SQL statement manually. I was wondering if there is any way I can code it to simply insert the entire datatable into SQL Server? Or some short-cut like this.

By the way I already tried doing this with SSIS, but there are a few data clean-up issues which I can handle with C# but not so easily with SSIS. The data started as XML, but I changed it to CSV for simplicity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-07-23 14:29:59

创建一个实现 IDataReader 的类“CsvDataReader”。 只需实现 Read()、GetValue(int i)、Dispose() 和构造函数:如果需要,您可以让其余部分抛出 NotImplementedException,因为 SqlBulkCopy 不会调用它们。 使用 read 处理每一行的读取,并使用 GetValue 读取该行中的第 i 个值。

然后将其与所需的适当列映射一起传递给 SqlBulkCopy。

使用该方法,我获得了大约 30000 条记录/每秒的插入速度。

如果您可以控制源文件格式,请将其设置为制表符分隔,因为它比 CSV 更容易解析。

编辑: http://www.codeproject.com/KB/database/CsvReader.aspx< /a> - tx 马克·格拉维尔。

Make a class "CsvDataReader" that implements IDataReader. Just implement Read(), GetValue(int i), Dispose() and the constructor : you can leave the rest throwing NotImplementedException if you want, because SqlBulkCopy won't call them. Use read to handle the read of each line and GetValue to read the i'th value in the line.

Then pass it to the SqlBulkCopy with the appropriate column mappings you want.

I get about 30000 records/per sec insert speed with that method.

If you have control of the source file format, make it tab delimited as it's easier to parse than CSV.

Edit : http://www.codeproject.com/KB/database/CsvReader.aspx - tx Mark Gravell.

够钟 2024-07-23 14:29:59

SqlBulkCopy(如果可用)。 这是关于在 ADO.NET 2.0 中使用 C# 使用 SqlBulkCopy 的非常有用的解释

我认为您可以将 XML 直接加载到 DataSet 中,然后将 SqlBulkCopy 映射到数据库和 DataSet。

SqlBulkCopy if it's available. Here is a very helpful explanation of using SqlBulkCopy in ADO.NET 2.0 with C#

I think you can load your XML directly into a DataSet and then map your SqlBulkCopy to the database and the DataSet.

趴在窗边数星星i 2024-07-23 14:29:59

嘿,您应该恢复为 XML 而不是 csv,然后使用 openxml 将 xml 文件加载到临时表中,清理临时表中的数据,最后处理该数据。

我一直在遵循这种方法进行大量数据导入,其中我的 XML 文件恰好是 > 500 mb 的大小和 openxml 的工作就像一个魅力。

您会惊讶地发现,与手动 ado.net 语句相比,它的运行速度要快得多。

Hey you should revert back to XML instead of csv, then load that xml file in a temp table using openxml, clean up your data in temp table and then finally process this data.

I have been following this approach for huge data imports where my XML files happen to be > 500 mb in size and openxml works like a charm.

You would be surprised at how much faster this would work compared to manual ado.net statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文