3

I have a csv file with 3 million lines and want to stored it in a database using c#. The csv file looks like "device;date;value".

Shall I write it into an array or directly into a System.Data.DataTable? And what is the fastest way to store this DataTable into a Database (SQL-Server for example).

I tried to store the lines using 3 million insert into statements but it was too slow :)

thanks

2

4 Answers 4

4

You can load the data in a DataTable and then use SqlBulkCopy for copying the date to the table in sql server

The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance. .

Sign up to request clarification or add additional context in comments.

Comments

0

I'd guess BCP would be pretty fast. Once you have the data in a DataTable you can try

using (SqlBulkCopy bcp= new SqlBulkCopy(yourConnectionString))
{
    BulkCopy.DestinationTableName = "TargetTable";
    BulkCopy.WriteToServer(dataTable);
}

Comments

0

I think the best way is to open stream reader and create row line by line. Use ReadLine in a while loop and split to find differents parts.

Comments

0

Sending 3 millions insert statements is bordering on crazy slow!

Buffer it by using transactions and reading in for example 200-1000 lines at a time (the smaller your data, the more you can read in at a time) then, after reading in these lines, commit your inserts to the database directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.