Bulk insert questions

只谈情不闲聊 提交于 2020-01-01 07:09:20

问题


I have a CSV file at the client side, and I want to develop a C# application to bulk insert the data into a table of a database to minimal log output. I am confused about if I use ADO.NET at the client side to call stored procedures in the database server. What kind of code needs to develop at the client side and what kind of code needs to be implemented at the server side in the form of stored procedures?

But I did not find any samples from Google. What are some ready to use samples? :-)

EDIT: Some more information:

I have a lot of data at the client side and I want to import to the database, but I do not want the overhead of all the many transaction logs. For security reasons, I want to develop a stored procedure at the server side and call from client side (ADO.NET). I want to know to achieve such goal. What kind of T-SQL needs to be developed in stored procedures at the server side and how to call/fill data efficiently at the client side?

If anything is still unclear, please feel free to let me know.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/662179/bulk-insert-questions

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