One, use EF Insert, Time 8135ms.
1Stopwatch Stopwatch =NewStopwatch ();2 Stopwatch.start ();3 using(varContent =NewHardwarecontext ())4 {5 for(inti = -; I < +; i++)6 {7hardwarelimited hardwarelimited =Newhardwarelimited8 {9ID =I,TenDataid ="00000000000502", OneIsenable =true A }; - content. Hardwarelimiteds.add (hardwarelimited); - } the content. SaveChanges (); - } - stopwatch.stop (); -MessageBox.Show (StopWatch.ElapsedMilliseconds.ToString ());
Second, direct insertion, time 1.745ms.
1 stringsql ="";2Stopwatch Stopwatch =NewStopwatch ();3 Stopwatch.start ();4 for(inti = -; I < +; i++)5 {6SQL + ="INSERT INTO [Projectts]. [dbo]. [Mshardwarelimited] (id,dataid,isenable) VALUES ('"+i.tostring () +"' , ' 00000000000502 ', 1)";7 }8 dbhelp.executesql (SQL);9 stopwatch.stop ();TenMessageBox.Show (StopWatch.ElapsedMilliseconds.ToString ());
Third, use SqlBulkCopy Insert, Time 244ms.
1DataTable datatable =NewDataTable ();2DATATABLE.COLUMNS.ADD ("ID",typeof(int));3DATATABLE.COLUMNS.ADD ("Dataid",typeof(string));4DATATABLE.COLUMNS.ADD ("isenable",typeof(BOOL));5 for(inti = -; I < +; i++)6 {7DataRow datarow =Datatable.newrow ();8datarow["ID"] =i;9datarow["Dataid"] ="00000000000502";Tendatarow["isenable"] =true; One DataTable.Rows.Add (dataRow); A } -Stopwatch Stopwatch =NewStopwatch (); - Stopwatch.start (); the using(SqlConnection conn =NewSqlConnection (dbhelp.connectstring)) - { - Conn. Open (); - using(SqlBulkCopy bulk =NewSqlBulkCopy (dbhelp.connectstring)) + { -Bulk. BatchSize =10000; +Bulk. DestinationTableName ="[Projectts]. [dbo]. [Mshardwarelimited]"; ABulk. Columnmappings.add ("ID","ID"); atBulk. Columnmappings.add ("Dataid","Dataid"); -Bulk. Columnmappings.add ("isenable","isenable"); - Bulk. WriteToServer (dataTable); - } - Conn. Close (); - Conn. Dispose (); in } - stopwatch.stop (); toMessageBox.Show (StopWatch.ElapsedMilliseconds.ToString ());
Database insertion Speed Comparison