1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Diagnostics;6 usingSystem.Data;7 usingSystem.Data.SqlClient;8 usingSystem.Configuration;9 usingFx678Member.Framework.Exceptions;Ten One namespaceMeiYuanJinYe.Admin.HttpHandler A { - /// <summary> - ///Summary description of CreateAccount the /// </summary> - Public classCreateaccount:ihttphandler - { - + Public voidProcessRequest (HttpContext context) - { +Context. Response.ContentType ="Text/plain"; AGuid Classroomid = guid.parse (context. request["Classroomid"]); at intCount =int. Parse (context. request["Count"]); -DataTable dt =GetTableSchema (); -Random ran =NewRandom (); - for(inti =0; i < Count; i++)//loop to assign values in a DataTable - { -DataRow r =dt. NewRow (); inr[1] = ran. Next (10000000,100000000); -r[2] = ran. Next (10000000,100000000); tor[3] =Classroomid; +r[4] =DateTime.Now; -r[5] =1; the dt. Rows.Add (r); * } $ bulktodb (DT);Panax NotoginsengContext. Response.Write (Bulktodb (DT)?"OK":"Error"); -Context. session["dataTable"] =DT; the } + A the Public voidBulktodb (DataTable DT) + { -SqlConnection sqlconn =NewSqlConnection (configurationmanager.appsettings["connstring"]); $SqlBulkCopy bulkcopy =NewSqlBulkCopy (sqlconn); $Bulkcopy.destinationtablename ="Classroomaccount";//database table name -Bulkcopy.batchsize =dt. Rows.Count; - Try the { - Sqlconn.open ();Wuyi if(dt! =NULL&& dt. Rows.Count! =0) the bulkcopy.writetoserver (DT); - } Wu Catch(Exception ex) - { About NewAppexception ("batch generate Live Room account exception", ex); $ } - finally - { - sqlconn.close (); A if(BulkCopy! =NULL) + bulkcopy.close (); the } - } $ the PublicDataTable GetTableSchema () the { theDataTable dt =NewDataTable (); theDt. Columns.addrange (Newdatacolumn[]{ - NewDataColumn ("AccountId",typeof(int)), in NewDataColumn ("AccountName",typeof(string)), the NewDataColumn ("Password",typeof(string)), the NewDataColumn ("Classroomid",typeof(Guid)), About NewDataColumn ("adddate",typeof(DateTime)), the NewDataColumn ("IsActive",typeof(int)) the});//database table Structure the returnDT; + } - Public BOOLisreusable the {Bayi Get the { the return false; - } - } the } the}
ASP. NET inserts data in bulk using bulk