How to Realize Batch Update in SqlCommandBuilder

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

If you use the designer to drag SqlDataAdapter onto the page, there will be no SqlDataAdapter. Update (ds) update error, because the system will automatically generate SqlDataAdapter property commands, such as:. UpdateCommane insertCommand selectCommand. But some programmers don't like to use designers, or some places don't need to drag such a huge thing as SqlDataAdapter to implement it, so SqlDataAdapter won't automatically generate related queries or update statements. So when the SqlDataAdapter. Update (ds) statement is executed, the SqlDataAdapter bridge does not know which table to update without reporting an error.
Solution: Realize batch update with SqlCommandBuilder
1. Functions:
You can implement DataSet in UI layer after any operation, directly throw to this method, this method can automatically update your changes to the database, and do not need to update to the database every time
2. How to use it


 public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
 {
  try
 {
  SqlConnection conn = new SqlConnection(strConnection));
  SqlDataAdapter myAdapter = new SqlDataAdapter();
  SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);  
  myAdapter.SelectCommand = myCommand;
  SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);  
  myAdapter.Update(ds,strTblName); 
  return 0;
}
catch(BusinessException errBU)
{ 
  throw errBU;
} 
catch(Exception err)
{ 
  throw new BusinessException(err);
}
}

Just call this method directly. The one point explained is that select * from "+ strTblName is a must, and everyone should think of the role, mainly telling SqlDataAdapter which table to update.
3. When will you use it?
a. Sometimes caching is needed, for example, in a product selection interface, after selecting products and editing/deleting/updating,
The last 1 is handed over to the database instead of accessing the database every 1 step, because the product selected by the customer may be edited/deleted n times
If the update operation is submitted every time, it will not only easily cause database conflicts and cause errors, but also be executed by users when the amount of data is large
It has also become a little slow in efficiency
b. Some interfaces are like this. Some interfaces are like this. Requirement 1 is implemented by cache. Confirm that the previous operations are not submitted to the library. Click
Submit item selection information and other information about the item only when the button on the page is dedicated to Submit. I often encounter this situation
c. In some cases, it only updates to the database and does not read. That is to say, it does not read from the database, so SqlDataAdapter does not know that it is
Which table has been updated, it is very likely that there will be an error in calling Update. In this case, SqlCommandBuilder can be used.
4. Note:
1). You can update only 1 table, not two or more associated tables
2). The table must have a primary key
3). The fields in the updated table cannot have image type
5. Advantages:
Save code and time, this method can replace all: update/delete/insert operation statements
6. Disadvantages:
It is a bit slow to access the database twice (select * TableName, that is, to confirm which table it is, unless it is a large amount of data, which is generally imperceptible).

The above is the method of batch update with SqlCommandBuilder, and introduces its advantages and disadvantages, as well as points for attention. The content is very detailed, and I hope everyone can like it.


Related articles: