Changing Database Records
So far, you’ve retrieved data from a database, but you haven’t manipulated its records in any way. Using ADO.NET, it is of course possible to add records, change an existing record, or delete a record altogether.
In a typical implementation, you might work your way through the following steps:
Fill the tables for your
DataSetusing a stored procedure or SQL.Display the data in various
DataTableobjects within yourDataSetby either binding to a control or looping through the rows in the tables.Change data in individual
DataTableobjects by adding, modifying, or deletingDataRowobjects.Invoke the
GetChanges( )method to create a secondDataSetthat features only the changes to the data.Check for errors in the second newly created
DataSetby examining theHasErrorsproperty. If there are errors, check theHasErrorsproperty of eachDataTablein theDataSet. If the table has errors, invoke theGetErrors( )method of theDataTableand get back an array ofDataRowobjects with errors. On each row you can examine theRowErrorproperty for specific information about the error, which you can then resolve.Merge the second
DataSetwith the first.Call the
Update( )method on theDataAdapterobject and pass in the mergedDataSet.Invoke the
AcceptChanges( )method on theDataSet, or invokeRejectChanges( )to cancel the changes.
This process gives you very fine control over the update to your data as well as an opportunity to fix any data that would otherwise ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access