usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;namespacesqlserverbulkinsert{classProgram { Public Static voidBulktodb (DataTable dt) {SqlConnection sqlconn=NewSqlConnection (configurationmanager.connectionstrings["connectionString"]. ConnectionString); SqlBulkCopy bulkcopy=NewSqlBulkCopy (sqlconn); Bulkcopy.destinationtablename="bulktesttable"; Bulkcopy.batchsize=dt. Rows.Count; Try{sqlconn.open (); if(dt! =NULL&& dt. Rows.Count! =0) bulkcopy.writetoserver (DT); } Catch(Exception ex) {Throwex; } finally{sqlconn.close (); if(BulkCopy! =NULL) Bulkcopy.close (); } } Public StaticDataTable GetTableSchema () {DataTable dt=NewDataTable (); Dt. Columns.addrange (Newdatacolumn[]{NewDataColumn ("Id",typeof(int)), NewDataColumn ("UserName",typeof(string)), NewDataColumn ("PWD",typeof(string))}); returnDT; } Static voidMain (string[] args) {Stopwatch SW=NewStopwatch (); for(intMultiply =0; Multiply <Ten; multiply++) {DataTable dt=GetTableSchema (); for(intCount = Multiply *100000; Count < (multiply +1) *100000; count++) {DataRow R=dt. NewRow (); r[0] =count; r[1] =string. Format ("user-{0}", Count *multiply); r[2] =string. Format ("pwd-{0}", Count *multiply); Dt. Rows.Add (R); } SW. Start (); BULKTODB (DT); Sw. Stop (); Console.WriteLine (string. Format ("Elapsed time is {0} Milliseconds", SW. Elapsedmilliseconds)); } console.readline (); } }}//------------------The following is an SQL statement for the test database and table ————————————————————————————Use [Bulktestdb]go/** * * * object:table [dbo]. [Bulktesttable] Script date:09/22/2016 09:38:04 * * * * **/SET ansi_nulls ongoset quoted_identifier ongoset ansi_padding ongocreate TABLE [dbo]. [Bulktesttable] ([Id] [int] Not NULL, [UserName] [nvarchar] ( +) NULL, [PWD] [varchar] ( -) null,primary KEY CLUSTERED ([Id] ASC) with (Pad_index= off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, Allow_page_locks =On ) On [PRIMARY]) on [Primary]goset ansi_padding Offgo
From:http://www.cnblogs.com/rengke2002/p/5895004.html
C # BULK INSERT Database Demo