public static void CopyData(DataTable sourceTable, string dbTable)
{
using (var cn = new SqlConnection(Database.ConnectionString))
{
cn.Open();
using (var s = new SqlBulkCopy(Database.ConnectionString))
{
s.DestinationTableName = dbTable;
s.NotifyAfter = 10000;
s.SqlRowsCopied += SqlRowsCopied;
s.WriteToServer(sourceTable);
s.Close();
}
}
}
private static void SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
Console.WriteLine("-- Copied {0} rows.", e.RowsCopied);
}
The datatable 'sourceTable' is formatted to exactly match the SQL table (name, data type etc). The method SqlRowsCopied just keeps you updated as to where it is in the insert process.
With this method you can easily insert all 750k records within a matter of seconds.
No comments:
Post a Comment