Asp. Net uses Bulk to insert data in batches. asp. netbulk
This article describes how Asp. Net uses Bulk to insert data in batches. The specific method is as follows:
The main function code is as follows:
Using System; using System. collections. generic; using System. linq; using System. web; using System. diagnostics; using System. data; using System. data. sqlClient; using System. configuration; using Fx678Member. framework. exceptions; namespace MeiYuanJinYe. admin. httpHandler {// <summary> // summary of CreateAccount /// </summary> public class CreateAccount: IHttpHandler {public void ProcessRequest (HttpContext cont Ext) {context. response. contentType = "text/plain"; Guid classRoomId = Guid. parse (context. request ["ClassRoomId"]); int Count = int. parse (context. request ["Count"]); DataTable dt = GetTableSchema (); Random ran = new Random (); for (int I = 0; I <Count; I ++) // cyclically assign {DataRow r = dt to the able. newRow (); r [1] = ran. next (10000000,100 running 00); r [2] = ran. next (10000000,100 running 00); r [3] = classRoomId; r [4] = DateTime. Now; r [5] = 1; dt. Rows. Add (r);} BulkToDB (dt); context. Response. Write (BulkToDB (dt )? "OK": "error"); context. session ["dataTable"] = dt;} public void BulkToDB (DataTable dt) {SqlConnection sqlConn = new SqlConnection (ConfigurationManager. appSettings ["ConnString"]); SqlBulkCopy bulkCopy = new SqlBulkCopy (sqlConn); bulkCopy. destinationTableName = "ClassRoomAccount"; // database table name bulkCopy. batchSize = dt. rows. count; try {sqlConn. open (); if (dt! = Null & dt. Rows. Count! = 0) bulkCopy. writeToServer (dt);} catch (Exception ex) {new AppException ("batch generation of live broadcasting room account exceptions", ex);} finally {sqlConn. close (); if (bulkCopy! = Null) bulkCopy. close () ;}} public DataTable GetTableSchema () {DataTable dt = new DataTable (); dt. columns. addRange (new DataColumn [] {new DataColumn ("AccountId", typeof (int), new DataColumn ("AccountName", typeof (string), new DataColumn ("Password ", typeof (string), new DataColumn ("ClassRoomId", typeof (Guid), new DataColumn ("AddDate", typeof (DateTime), new DataColumn ("IsActive ", typeof (int)}); // return dt;} public bool IsReusable {get {return false ;}}}}
I hope this article will help you design your asp.net program.
Aspnet batch data insertion
Separate multiple names in Textbox1 using the split method.
Textbox1.Text. Split (",")
This method returns a string array. Each array element contains a name. Then, traverse the elements of this array and insert them into the database respectively.
How does ASPNET implement batch data input?
A little more. Give it a score.
Public bool AddReport (Ap_Report model, List <Ap_ReportPrduct> list)
{
SqlConnection conn = new SqlConnection (SqlHelper. ConnectionString );
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
SqlTransaction tran = conn. BeginTransaction ();
Cmd. Transaction = tran;
SqlParameter [] par;
SqlParameter id;
Try
{
SqlParameter activityId;
If (model. ActivityId = null)
{
ActivityId = new SqlParameter ("@ ActivityId", DBNull. Value );
}
Else
{
ActivityId = new SqlParameter ("@ ActivityId", model. ActivityId );
}
SqlParameter CreateTime;
If (model. CreateTime = null)
{
CreateTime = new SqlParameter ("@ CreateTime", DBNull. Value );
}
Else
{
CreateTime = new SqlParameter ("@ CreateTime", model. CreateTime );
}
SqlParameter BeginTime;
If (model. ActivityBeginTime = null)
{
BeginTime = new SqlParameter ("@ ActivityBeginTime", DBNull. Value );
}
Else
{
BeginTime = new SqlParameter ("@ A ...... the remaining full text>