1 usingSystem;2 usingSystem.Configuration;3 usingSystem.Data;4 usingSystem.Data.SqlClient;5 usingSystem.Diagnostics;6 7 namespaceSqlBulkCopy18 {9 class ProgramTen { One Static voidMain (string[] args) A { -Stopwatch SW =NewStopwatch (); - for(intMultiply =0; Multiply <Ten; multiply++) the { -DataTable dt =GetTableSchema (); - for(intCount = Multiply *100000; Count < (multiply +1) *100000; count++) - { +DataRow r =dt. NewRow (); -r[0] =count; +r[1] =string. Format ("username-{0}", Count *multiply); Ar[2] =string. Format ("userpwd-{0}", Count *multiply); atr[3] =string. Format ("address-{0}", Count *multiply); - dt. Rows.Add (r); - } - SW. Start (); - bulktodb (DT); - SW. Stop (); inConsole.WriteLine (string. Format ("Elapsed Time {0} milliseconds", SW. Elapsedmilliseconds)); - } to console.readline (); + } - Public Static voidBulktodb (DataTable DT) the { *SqlConnection con =NewSqlConnection (configurationmanager.connectionstrings["sqlconnstring"]. ConnectionString); $SqlBulkCopy bulkcopy =NewSqlBulkCopy (con);Panax NotoginsengBulkcopy.destinationtablename ="USERINFO"; -Bulkcopy.batchsize =dt. Rows.Count; the Try + { A con. Open (); the if(dt! =NULL&& dt. Rows.Count! =0) + bulkcopy.writetoserver (DT); - } $ Catch(Exception) $ { - - Throw; the } - }Wuyi the Public StaticDataTable GetTableSchema () - { WuDataTable dt =NewDataTable (); -Dt. Columns.addrange (Newdatacolumn[] { About NewDataColumn ("ID",typeof(int)), $ NewDataColumn ("username",typeof(string)), - NewDataColumn ("userpwd",typeof(string)), - NewDataColumn ("Address",typeof(string)) - }); A returnDT; + } the } -}
100W Data consumption time:
Bulk copying big Data using the SqlBulkCopy class