"Bulk join"-stitching SQL strings

Source: Internet
Author: User

Now do a project need to use bulk join, 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.

Implement the process in detail: first store the required data in the list of entities, and then pass the list to our bulk join method. The bulk join method should first open the database connection, and then start the concatenation of the SQL string, after stitching finished. Call the SqlHelper method to join.

Detailed code such as the following:

        #region Bulk Join entities///<summary>//Bulk 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;password=xxx; ", True);            StringBuilder sbsql = new StringBuilder (); Splicing string, 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 join. int flag = SqlHelper.            ExecuteNonQuery (Sbsql.tostring (), System.Data.CommandType.Text);            if (Flag > 0) {return true;            } else {return false; }} #endregion

When I started to write my own SQL stitching string, I thought, wow, this must be particularly difficult. I've heard it, I've never seen it before. The result is seen below. Found to be very simple.

So. Not afraid don't know, afraid don't know! Come on, guys.

"Bulk join"-stitching SQL strings

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.