| Using ADO .NET - More DB Updating - Part 5 | |
| 4 - The CommandBuilder | |
|
The CommandBuilder object is also new! This object creates the appropriate SQL action query (add, change or delete) based on a comparison of the information you're sending to the database and the information the database currently holds. Very convenient! But it's also poorly understood. (Many $50 books claiming to completely cover ADO .NET barely mention it.) The CommandBuilder object actually uses an UPDATE command that looks like this for the Microsoft.Jet.OLEDB.4.0 provider: UPDATE Articles SET artTitle = ? , artDate = ? , artDescription = ? , artVB6 = ? , artVBNET = ? , artURL = ? WHERE ( (artKey = ?) AND ((? IS NULL AND artTitle IS NULL) OR (artTitle = ?)) AND ((? IS NULL AND artDate IS NULL) OR (artDate = ?)) AND ((? IS NULL AND artDescription IS NULL) OR (artDescription = ?)) AND ((? IS NULL AND artVB6 IS NULL) OR (artVB6 = ?)) AND ((? IS NULL AND artVBNET IS NULL) OR (artVBNET = ?)) ) If you're using a different provider, the SQL command text will not be the same for your program. You can view your own SQL string with a line of code like this in your program: MsgBox(myCommand.GetUpdateCommand.CommandText.ToString) Notice that the command simply updates EVERY field (except the key) if a change is present. This VERY GENERIC and, in some ways, brute force approach does have limitations. An excellent article about limitations and alternatives to CommandBuilder was written by William Vaughn for MSDN Technical Articles and can be viewed here. When the button is clicked the first time, an If statement causes this block of code to be executed. Any changes to the initial values in the TextBox and CheckBox components are simply copied to the DataTable for updating. Notice the use of a Try-Catch block to make sure that the contents of the date field are valid for the Access database. Some work on helping other fields - the URL field comes to mind - could be done too but this example is being kept simple. Since a new row was added to myDataTable, the DataAdapter Update method inserts this row into the Access database. The RefreshListBox subroutine that is called at the end is new too! I suggested at the beginning that you will want to think long and hard about the way you manage the transfer of data between your local DataSet and the actual database. In this case, I chose to refresh the entire thing each time to ensure independence. This isn't the only way to accomplish this but it helps to make the points clear in this tutorial. The RefreshListBox code is as follows: One of the last statements in this clause of the If block is a change to the Button caption which is also used to control flow in the program. The next time the Button is clicked, the second clause is executed and data in the database is changed instead: ADO .NET conveniently provides a method to create a DataSet with only the changes necessary to be updated. newDataSet = myDataSet.GetChanges() And only these changes are actually sent to the database. myOleDbDataAdapter.Update(newDataSet, "Articles") Next page >
Autonumber Complications > Page
1,
2,
3,
4,
5,
6 |
|

