将非常大的CSV文件加载到SQL Server数据库中 [英] Load very big CSV-Files into s SQL-Server database

查看:192
本文介绍了将非常大的CSV文件加载到SQL Server数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个高性能的方式来加载到.NET的SQL Server 2008数据库中的非常大的CSV文件(大小几个千兆字节)?

Is there a performant way to load very big CSV-files (which have size of several gigabytes) into an SQL-Server 2008 database with .NET?

推荐答案

我会将此CSV阅读器与< a href =http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx =noreferrer> SqlBulkCopy ;

I would combine this CSV reader with SqlBulkCopy; i.e.

using (var file = new StreamReader(path))
using (var csv = new CsvReader(file, true)) // true = has header row
using (var bcp = new SqlBulkCopy(connection)) {
    bcp.DestinationTableName = "TableName";
    bcp.WriteToServer(csv);
}

这使用批量复制API来执行插入, - 管理(和快速) IDataReader 实现(至关重要的是,数据,而不是一次性加载所有数据)。

This uses the bulk-copy API to do the inserts, while using a fully-managed (and fast) IDataReader implementation (crucially, which streams the data, rather than loading it all at once).

这篇关于将非常大的CSV文件加载到SQL Server数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆