string sql='
select a.e_no,a.e_name,a.e_dept,b.b_deptname from a,b where a.e_dept =b.b_dept ';
user the sql above to fill a dataset,if i wanna to insert a row on datagridveiw,or chage some data on datagridview ,how can i use to update tabele a,
sda.update(ds)?
kare vamPosted Oct 22, 2007, 2:01 AM
The two connected and disconnected models of ADO.NET work very differently when updating a database. Connected providers communicate with the database by using command based updates. Disconnected DataSets update the database by using a cached, batch-optimistic method. DataSets work independently from a connection. Changes are committed to the Database via the DataAdapter.
Managing DataSet Changes
Because the DataSet is inherently disconnected from the datasource, it must manage its changes by itself. The DataSet supports several flags that indicate whether changes have occurred. These flags come in the form of the GetChanges() and HasChanges() methods, which enable it to reconcile changes back to its datasource via the DataAdapter object. The following code shows the usage of these two methods.
C# 2005
if (myAccountDataset.HasChanges())
{
MessageBox.Show("Has Changes.");
dstchanges = myAccountDataset.GetChanges();
if (dstchanges.HasErrors)
{dstchanges.RejectChanges();}
else{myDataset.AcceptChanges();}
}
Detecting and handling changes to data in a DataSet
It is necessary to know if the data in DataSet has been changed. Changes can be new rows added, rows deleted and rows modified. The DataSet class includes the HasChanges() Method which is used for this purpose. The HasChanges() Method () is called before GetChanges() method is called. In the code given above, the statement dstchanges=myDataset.GetChanges(), saves the data changes into a new DataSet (dstchanges). The statement dstchanges.HasErrors checks whether data in the DataSet has any errors. The GetChanges() method creates a DataSet containing the changes made to it since the last time we called the AcceptChanges() method.
Accepting or Rejecting changes to data in a DataSet
When changes have been made to data in the DataSet, we can choose to reject or accept them.
The DataSet class has two methods for this purpose: The AcceptChanges() and RejectChanges().
The AcceptChanges() Method : The AcceptChanges() Method commits the modifications to a DataSet.
The RejectChanges() Method : The RejectChanges() method deletes the newly added row or reverses the changes made to an existing row.
When we are finished manipulating the data in the DataSet, it is time to update the data source. This is done using the Update method of the DataAdapter class. This method is responsible for examining the RowState property of each of the DataRow objects in the Rows collection. The Rows collection is a member of each of the DataTable objects contained in the Tables collection of
the DataSet. The DataAdapter starts by looping through all the tables in the Tables collection in the DataSet, and for each table it loops through the Rows collection to examine the RowState property. If a row has been inserted, updated or deleted, the DataAdapter uses one of the command properties to handle the update. The InsertCommand property is used if we are inserting a new row, the UpdateCommand property is used if we are updating an existing row, and the DeleteCommand property is used if we are deleting an existing row. The statement given below uses the Update method to update the changes to the Database.
C#2005
myAccountAdapter.Update(myAccountDataset.GetChanges(), "AccountsTable");