Method for batch updating excel or CSV data by SqlCommandBuilder class

  • 2021-07-06 10:46:21
  • OfStack

When you work with data in batches, you often use update table1 from table2, which is the simplest way.
But when you update massive data from excel or CSV files, is it good to use this method? How many strings are spliced, does big data need to be updated in groups?
I don't want to test the speed of various methods, because I personally agree with the following methods, and welcome your criticism and correction.
The class I need to use is mainly SqlCommandBuilder.


/// <param name="table"> Prepare for update DataTable New data </param>
  /// <param name="TableName"> Corresponding to the database table name to be updated </param>
  /// <param name="primaryKeyName"> The primary key name corresponding to the database table to be updated </param>
  /// <param name="columnsName"> Collection of column names corresponding to the column to be updated </param>
  /// <param name="limitColumns"> The condition string that needs to be qualified in the WHERE condition of SQL, which can be empty. </param>
  /// <param name="onceUpdateNumber"> Number of rows processed per round trip </param>
  /// <returns> Returns the number of updated rows </returns>
  public static int Update(DataTable table, string TableName, string primaryKeyName, string[] columnsName, string 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();

      // Transactions using enhanced read-write locks   
      SqlTransaction tran = sqlconn.BeginTransaction(IsolationLevel.ReadCommitted);
      try
      {
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
          // All rows are set to modified status   
          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 settings parameters that need to be updated are , The parameter is named "@+ Column name "
        for (int i = 0; i < columnsName.Length; i++)
        {
          // The column name to be updated and its parameter values are spliced here 
          columnsUpdateSql += ("[" + columnsName[i] + "]" + "=@" + columnsName[i] + ",");
          paras[i] = new SqlParameter("@" + columnsName[i], columnsName[i]);
        }
        if (!string.IsNullOrEmpty(columnsUpdateSql))
        {
          // Remove the splice here and finally 1 A ","
          columnsUpdateSql = columnsUpdateSql.Remove(columnsUpdateSql.Length - 1);
        }
        // Generate here where Conditional statement 
        string limitSql = ("[" + primaryKeyName + "]" + "=@" + primaryKeyName);
        SqlCommand updateCmd = new SqlCommand(string.Format(" UPDATE [{0}] SET {1} WHERE {2} ", TableName, columnsUpdateSql, limitSql));
        // Do not modify the source DataTable  
        updateCmd.UpdatedRowSource = UpdateRowSource.None;
        da.UpdateCommand = updateCmd;
        da.UpdateCommand.Parameters.AddRange(paras);
        da.UpdateCommand.Parameters.Add("@" + primaryKeyName, primaryKeyName);
        // Number of rows processed 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 called, including the primary key name and column name, should correspond to the actual name of the database.
You don't pass in the qualified where condition. If you pass in, you only need to pass in: Name= "chamy" or Name= "jundy". You don't need to add characters such as "where". You can't pass in the qualification of primary key here. You only need to pass in the name on the parameter of primary key name.

The above is the method of updating excel or CSV data in batches for everyone to share, hoping to help everyone learn.


Related articles: