Bulk copying big Data using the SqlBulkCopy class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.