Bulk insert questions

泪湿孤枕 提交于 2019-12-03 20:59:43

You can hook CsvReader to SqlBulkCopy, which does the job very nicely... something like (untested):

using (CsvReader reader = new CsvReader(path))
using (SqlBulkCopy bcp = new SqlBulkCopy(CONNECTION_STRING))
{
    bcp.DestinationTableName = "SomeTable";
    bcp.WriteToServer(reader);
}

edit you would typically do the bulk-insert into a staging table, and then use a regular stored procedure to move the data into the real table.

Paul Suart

Are you using SQL Server 2008? And are you able to execute dynamic SQL (not that I'm advocating it)?

If so, you could construct an insert statement that makes use of "Row contructors". Essentially an insert statement will now accept an array of arguments for each row, like so:

INSERT INTO TableA (Col1, Col2)
VALUES ('A', 'B'), ('C', 'D')

There's more about it in the blog post "SQL Server 2008 – Insert Multiple Records Using One Insert Statement – Use of Row Constructor".

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!