Updating data in a dataset

The Add New method of the Data Table adds a row to a Data Table.

To change an existing row's data, you can directly assign a row's value. Delete() In this example, I'll show you how to add edit, and remove, data from the Northwind database.

updating data in a dataset-76

Note: Keep in mind that this Update is different than a SQL UPDATE statement.

To insert, update, and delete data using a Data Adapter, you create an Ole Db Command object with INSERT, UPDATE, and DELETE statements and set the Insert Command, Update Command, and Delete Command properties of the data adapter to Ole Db Command.

Using the Data Adapters you can fill Data Set and use this dataset for retrieving and storing information.

When all the tasks get completed, update Real Database with datasets.

In a simple word, A Data Set is a local copy of your Database Table that gets populated in client PC.

It is independent of Data Source and because it exists in the local system, it makes application fast and reliable.

The reason for this is depending on how you you merge Data Sets or add rows to a Data Set the Identity column will have a place holder. Those are not replicated down to the Data Sets so we have to do this manually. Add(Parent Constraints); Pay attention to the Update Rule setting for the Foreign Key constraint. Foreign Key Constraint Parent FK = new Foreign Key Constraint("IDFK", My Data Set. This is not very efficient if you have multiple tables that need to be updated. This is very easy using the Merge method in your Destination Data Set, New Data being the Data Set I want to merge into my existing data. Add); Our Data Sets are now merged and ready to be Updated in the new location. The only thing to remember is that the Identity column is marked as Read Only, therefore you can't update it without first changing it.

This place holder will not necessarily be the same value as what the database will contain. The Unique constraint Unique Constraint Parent Constraints = new Unique Constraint(new Data Column[] ); My Data Set. In that case wrap the below foreach loop in another foreach loop to iterate through each table.foreach (Data Row row in New Data. You have to set preserve Changes to true or the Merge will throw out all your changes and nothing will make it into the new database. Sql Command Builder Parent Command Builder = new Sql Command Builder(Parent Adapter); Sql Command Builder Child Command Builder = new Sql Command Builder(Child Adapter); My Data Set.

You can avoid building Command objects manually by using Command Builder.

As you saw in the Data Table, Data Row, and Data Column sections at the beginning of this article, you can use the Delete method of a Data Row to delete a row.

Accessing Remote Database each time for updating or retrieving details are time-consuming so datasets help you to keep local database tables on your PC.

Tags: , ,