ASP. NET inserts data in bulk using bulk

Source: Internet
Author: User

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

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.