Asp. Net uses Bulk to insert data in batches and asp. netbulk to insert data
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. diagnostics; 6 using System. data; 7 using System. data. sqlClient; 8 using System. configuration; 9 using Fx678Member. framework. exceptions; 10 11 namespace MeiYuanJinYe. admin. httpHandler12 {13 /// <summary> 14 // summary of CreateAccount 15 /// </summary> 16 public class CreateAccount: IHttpHandler17 {18 19 public void ProcessRequest (HttpContext context) 20 {21 context. response. contentType = "text/plain"; 22 Guid classRoomId = Guid. parse (context. request ["ClassRoomId"]); 23 int Count = int. parse (context. request ["Count"]); 24 DataTable dt = GetTableSchema (); 25 Random ran = new Random (); 26 for (int I = 0; I <Count; I ++) // cyclically assign 27 {28 DataRow r = dt to the able. newRow (); 29 r [1] = ran. next (10000000,100 00 0000); 30 r [2] = ran. next (10000000,100 running 00); 31 r [3] = classRoomId; 32 r [4] = DateTime. now; 33 r [5] = 1; 34 dt. rows. add (r); 35} 36 BulkToDB (dt); 37 context. response. write (BulkToDB (dt )? "OK": "error"); 38 context. session ["dataTable"] = dt; 39} 40 41 42 public void BulkToDB (DataTable dt) 43 {44 SqlConnection sqlConn = new SqlConnection (ConfigurationManager. appSettings ["ConnString"]); 45 SqlBulkCopy bulkCopy = new SqlBulkCopy (sqlConn); 46 bulkCopy. destinationTableName = "ClassRoomAccount"; // database table name 47 bulkCopy. batchSize = dt. rows. count; 48 try49 {50 sqlConn. open (); 51 if (dt! = Null & dt. Rows. Count! = 0) 52 bulkCopy. writeToServer (dt); 53} 54 catch (Exception ex) 55 {56 new AppException ("batch generation of live broadcasting room account exceptions", ex); 57} 58 finally59 {60 sqlConn. close (); 61 if (bulkCopy! = Null) 62 bulkCopy. close (); 63} 64} 65 66 public DataTable GetTableSchema () 67 {68 DataTable dt = new DataTable (); 69 dt. columns. addRange (new DataColumn [] {70 new DataColumn ("AccountId", typeof (int), 71 new DataColumn ("AccountName", typeof (string )), 72 new DataColumn ("Password", typeof (string), 73 new DataColumn ("ClassRoomId", typeof (Guid), 74 new DataColumn ("AddDate ", typeof (DateTime), 75 new DataColumn ("IsActive", typeof (int) 76}); // database table structure 77 return dt; 78} 79 public bool IsReusable80 {81 get82 {83 return false; 84} 85} 86} 87}
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 to insert data in batches in C # net
There are many methods. I used the SQL statement in vs2005 to insert a table into another table as if it was a specific function.
You can put the data to be processed in batch into a snack table and then use this item.