I'm trying to make a service that will sometimes import 100 million rows from csv file to database.
My code works very slow (0.5 seconds for every request) and I have to make many requests.
I have two columns, they are both a primary keys. Therefore it can be so slow because of indexing. But if I make an import using Microsoft SQL Server Management Studio (import wizard) - it works very fast.
Is there a way to make it faster or with fewer requests?
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
long i = 0;
string result = "";
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
string[] data = s.Split(',');
sb.Append(string.Format("('{0}',{1}),", data[0], data[1]));
i++;
if (i % 1000 == 0)
{
result = sb.ToString();
string queryString = "USE[PassportExpiredTestBase]\r\n" +
"INSERT INTO[dbo].[PassportList]([Series],[Number])VALUES" +
result.Substring(0, result.Length - 1);
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery(); // The most expensive operation
sb.Clear();
}
}
connection.Close();
}