SqlCommandBuilder class method for batch updating of Excel or CSV data _ practical Tips

Source: Internet
Author: User

When you bulk manipulate data, you will often use this approach to update table1 from table2 , which is the simplest.
But when you update a mass of data from an Excel or CSV file, you use this method, is the performance good? string concatenation and how many, large data need to be grouped update?
I do not want to detect the speed of various methods, because I personally agree with the following ways, welcome criticism and corrections.
The classes I need to use are mostly SqlCommandBuilder .

<param name= "TABLE" > Preparing updated DataTable new data </param>///<param name= "tablename" > corresponding database table names to be updated </ param>///<param name= "Primarykeyname" > corresponds to the primary key name of the database table to be updated </param>///<param name= "Columnsname" > A set of column names corresponding to the column to be updated </param>///<param name= "Limitcolumns" > a condition string that needs to be qualified in the SQL where condition, nullable. </param>///<param name= "Onceupdatenumber" > Number of rows per round-trip </param>///<returns> returns the number of rows to update </ returns> public static int Update (DataTable table, string tablename, String primarykeyname, string[] columnsname, str ing Limitwhere,int onceupdatenumber) {if (string).
    IsNullOrEmpty (tablename)) return 0; if (string.
    IsNullOrEmpty (primarykeyname)) return 0;
    if (Columnsname = null | | columnsname.length <= 0) return 0;
    DataSet ds = new DataSet (); Ds.
    Tables.add (table);
    int result = 0; using (SqlConnection sqlconn = new SqlConnection (sqlhelper.connstring)) {sqlconn.

      Open (); Using enhanced read-write lock transaction Sqltransaction tran = sqlconn.
      BeginTransaction (isolationlevel.readcommitted); try {foreach (DataRow dr in DS.) Tables[0]. rows) {//All rows are set to modify state Dr.
        SetModified (); ///For Adapter location target table SqlCommand cmd = new SqlCommand (string.
        Format ("select * from {0} where {1}", Tablename,limitwhere), Sqlconn, Tran);
        SqlDataAdapter da = new SqlDataAdapter (cmd);
        SqlCommandBuilder Sqlcmdbuilder = new SqlCommandBuilder (DA); Da.
        AcceptChangesDuringUpdate = false;
        String columnsupdatesql = "";
        Sqlparameter[] paras = new sqlparameter[columnsname.length];
          The column setting parameter that needs to be updated is the parameter named "@+ column name" for (int i = 0; i < columnsname.length; i++) {//Where the column names to be updated and their parameter values are spliced
          Columnsupdatesql + + ("[" + Columnsname[i] + "]" + "=@" + columnsname[i] + ",");
        Paras[i] = new SqlParameter ("@" + columnsname[i], columnsname[i]); } if (!string.
IsNullOrEmpty (Columnsupdatesql)) {          The last "," Columnsupdatesql = Columnsupdatesql.remove (columnsupdatesql.length-1) is removed from the stitching place here.
        The WHERE condition statement string limitsql = ("[" + Primarykeyname + "]" + "=@" + primarykeyname) is generated. SqlCommand updatecmd = new SqlCommand (string.
        Format ("UPDATE [{0}] SET {1} WHERE {2}", TableName, Columnsupdatesql, Limitsql));
        Do not modify source datatable Updatecmd.updatedrowsource = Updaterowsource.none; Da.
        UpdateCommand = Updatecmd; Da.
        UpdateCommand.Parameters.AddRange (paras); Da.
        UPDATECOMMAND.PARAMETERS.ADD ("@" + primarykeyname, primarykeyname); The number of rows per round trip da.
        UpdateBatchSize = Onceupdatenumber; result = da.
        Update (Ds,tablename); Ds.
        AcceptChanges ();

      Tran.commit (); Catch {tran.
      Rollback (); finally {sqlconn.
        Dispose (); Sqlconn.
      Close ();
  } return result;

 }

Note: the parameters to be passed in when this method is invoked, including the primary key and column names, should correspond to the actual names of the databases.
You can not pass in the qualified where condition, if passed in, just pass in: Name= "Chamy" or name= "Jundy", do not need to add "where" and other characters, you can not pass in the primary key qualification, you only need to be in the primary key name this parameter on the name can be passed.

The above is for you to share the SqlCommandBuilder class batch update Excel or CSV data method, hope for everyone's learning help.

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.