Now do a project need to use bulk add, but the bottom of the package does not have this method, so self-reliance, to write. We use the method of stitching the SQL string to implement the function.
Implementation process: First store the required data in the list of entities, and then pass the list to our bulk-add method. The bulk Add method should first open the database connection, and then start the concatenation of the SQL string, after stitching is complete, call the SqlHelper method to add.
The specific code is as follows:
#region Bulk Add entities///<summary>///Bulk Add entities///</summary>//<param name= "Staffscores" ></param>///<returns></returns> public bool Insert (list<yzquerystaffs Coresentity> staffscores) {//Open database connection SQLHelper SQLHelper = new SQLHelper ("Data source=192 .168.22.239;initial catalog=yzevaluationsystem;persist Security Info=true;user Id=sa; [email protected]; ", true); StringBuilder sbsql = new StringBuilder (); Stitching strings, adding method Sbsql.append ("INSERT INTO yzquerystaffscoresentity (Id,staffid,staffname,workefficiency,professiona lethics,businessability,honestydiscipline,workperformance,other1,other2,number,totalscores,isused) ("the Values"); Loop entity list, stitching if (staffscores. Count > 1) {for (int i = 0; i < Staffscores. Count-1; i++) {sbsql.append ("('" + staffscores[i].id + "',"); Sbsql.append ("'" + staffscores[i). StaffID + "',"); Sbsql.append ("'" + staffscores[i). Staffname + "',"); Sbsql.append (Staffscores[i]. Workefficiency + ","); Sbsql.append (Staffscores[i]. Professionalethics + ","); Sbsql.append (Staffscores[i]. Businessability + ","); Sbsql.append (Staffscores[i]. Honestydiscipline + ","); Sbsql.append (Staffscores[i]. Workperformance + ","); Sbsql.append (Staffscores[i].other1 + ","); Sbsql.append (Staffscores[i].other2 + ","); Sbsql.append (Staffscores[i]. Number + ","); Sbsql.append (Staffscores[i]. Totalscores + ","); Sbsql.append (1); Sbsql.append ("),"); } sbsql.append ("('" + staffscores[staffscores. Count-1].id + "',"); Sbsql.append ("'" + staffscores[staffscores. CounT-1]. StaffID + "',"); Sbsql.append ("'" + staffscores[staffscores. COUNT-1]. Staffname + "',"); Sbsql.append (staffscores[staffscores. COUNT-1]. Workefficiency + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Professionalethics + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Businessability + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Honestydiscipline + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Workperformance + ","); Sbsql.append (staffscores[staffscores. Count-1].other1 + ","); Sbsql.append (staffscores[staffscores. Count-1].other2 + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Number + ","); Sbsql.append (staffscores[staffscores. COUNT-1]. Totalscores + ","); Sbsql.append (1); Sbsql.append (")"); }//Call the SqlHelper method to add. int flag = Sqlhelper.executenonquery (Sbsql.tostring (), System.Data.CommandType.Text); if (Flag > 0) {return true; } else {return false; }} #endregion
At the outset to write their own SQL splicing string when also think, wow, this must be particularly difficult, I have heard, never seen Ah, the results of a see, found very simple. So, not afraid do not know, afraid do not know! Come on, guys.
"Bulk Add"-stitching SQL strings