Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk insert questions

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.

like image 722
George2 Avatar asked Dec 06 '25 05:12

George2


1 Answers

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.

like image 180
Marc Gravell Avatar answered Dec 07 '25 19:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!