I used to insert big data one by one. Because the computer configuration was not good, it took me half an hour to insert 0.17 million data records. That hurts! The computer configured with 0.25 million pieces of data is completed in a few seconds. What is the magic code? I spent a lot of money to show off the Buddha.
I used to insert big data one by one. Because the computer configuration was not good, it took me half an hour to insert 0.17 million data records. That hurts! The computer configured with 0.25 million pieces of data is completed in a few seconds. What is the magic code? I spent a lot of money to show off the Buddha.
PreviousInsertLargeDataIt's always one by one.Insert, Because the computer configuration is not good, one 0.17 millionDataIt took about half an hour.InsertFinished. It hurts!
0.25 millionDataThe computer has been configured in a few seconds. What is the magic code?
I spent a lot of money to show off the Buddha.
DataThe T_Nums table has four fields:
NumSection, AddressStr, NumType, AreaCode
Then read the input data from the text file to a able, and thenInsertDataLibrary speed is too fast
Run:
Because the code is relatively simple and does not write comments, you may not like reading help documents or leaving a message if you do not understand it...
The following code is used:
String connStr = "Data Source = WADE-PC; Initial Catalog = Test; Persist Security Info = True; User ID = sa; Password = 123456 ";
DateTime startTime = DateTime. Now;
OpenFileDialog dialog = new OpenFileDialog ();
Dialog. Filter = "text file (*. txt) | *. txt ";
If (dialog. ShowDialog () = DialogResult. OK)
{
DataTable dt = new DataTable ();
Dt. Columns. Add ("NumSection ");
Dt. Columns. Add ("AddressStr ");
Dt. Columns. Add ("NumType ");
Dt. Columns. Add ("AreaCode ");
IEnumerable lines = File. ReadLines (dialog. FileName, Encoding. Default );
Foreach (string str in lines)
{
String [] strs = str. Split ('\ t ');
DataRow row = dt. NewRow ();
Row ["NumSection"] = strs [0];
Row ["AddressStr"] = strs [1]. ToString (). Trim ('"');
Row ["NumType"] = strs [2]. ToString (). Trim ('"');
Row ["AreaCode"] = strs [3]. ToString (). Trim ('"');
Dt. Rows. Add (row );
}
Using (SqlBulkCopy bulkCopy = new SqlBulkCopy (connStr ))
{
BulkCopy. DestinationTableName = "T_Nums ";
BulkCopy. ColumnMappings. Add ("NumSection", "NumSection ");
BulkCopy. ColumnMappings. Add ("AddressStr", "AddressStr ");
BulkCopy. ColumnMappings. Add ("NumType", "NumType ");
BulkCopy. ColumnMappings. Add ("AreaCode", "AreaCode ");
BulkCopy. WriteToServer (dt );
}
TimeSpan ts = DateTime. Now. Subtract (startTime );
MessageBox. Show ("TotalInsert"+ Dt. Rows. Count +"Data, Time: "+ ts. ToString ());
[Editor: peter]