views:

542

answers:

2

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.

+6  A: 

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.

Marc Gravell
Marc, please let me know when you found them. BTW, "hook" you mean call an external command/application?
George2
No external; all managed code. Example added, but I haven't time to test it (gotta run...)
Marc Gravell
I searched CsvReader, find a lot of tools are called CsvReader, but no Microsoft tools. Which CsvReader do you mean? :-)
George2
Hi Marc, in order to call WriteToServer from ADO.Net client, do we need any special configuration at server side, and any special permission? Thanks.
George2
Re "which tool", I hyperlinked it already... you do need CRUD access to the table, of course - I believe it needs SELECT+INSERT (but might want to check that)
Marc Gravell
+1  A: 

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.

Paul Suart
1. Could your solution be used in SQL Server 2005?2. If I need to use SQL Server 2005, any solutions, thedorko?
George2
Your solution does not solve the issue of reducing the transaction logs, just a solution to save typing. :-)
George2
1 - Unfortunately, no, this is Sql Server 2008 only.2- Granted :)
Paul Suart
Oh, it doesn't just save typing. If you have 10 rows to insert, you can do them in one command, rather than 10.
Paul Suart
Cool, thedorko! Do you have more recommended document to introduce this new feature? I am interested to read more.
George2