Updating dataset c#


















The Update method of the DataAdapter is called to resolve changes from a DataSet back to the data source. The DataSet instance is the DataSet that contains the changes that have been made, and the DataTable identifies the table from which to retrieve the changes. This allows you to maximize the performance of your ADO. NET application by specifying command syntax at design time and, where possible, through the use of stored procedures. You must explicitly set the commands before calling Update.

If Update is called and the appropriate command does not exist for a particular update for example, no DeleteCommand for deleted rows , an exception is thrown. This causes the rows affected count returned to be zero, which the DataAdapter interprets as a concurrency conflict. In this event, a DBConcurrencyException will be thrown.

Command parameters can be used to specify input and output values for an SQL statement or stored procedure for each modified row in a DataSet. For more information, see DataAdapter Parameters. It is important to understand the difference between deleting a row in a DataTable and removing the row.

When you call the Remove or RemoveAt method, the row is removed immediately. Any corresponding rows in the back end data source will not be affected if you then pass the DataTable or DataSet to a DataAdapter and call Update. When you use the Delete method, the row remains in the DataTable and is marked for deletion. If you then pass the DataTable or DataSet to a DataAdapter and call Update , the corresponding row in the back end data source is deleted.

For more information, see Generating Commands with CommandBuilders. You can control how the values returned from the data source are mapped back to the DataTable following a call to the Update method of a DataAdapter , by using the UpdatedRowSource property of a DbCommand object. By setting the UpdatedRowSource property to one of the UpdateRowSource enumeration values, you can control whether output parameters returned by the DataAdapter commands are ignored or applied to the changed row in the DataSet.

You can also specify whether the first returned row if it exists is applied to the changed row in the DataTable. The following table describes the different values of the UpdateRowSource enumeration and how they affect the behavior of a command used with a DataAdapter. The Update method resolves your changes back to the data source; however other clients may have modified data at the data source since the last time you filled the DataSet.

New rows will be added to the table, and updated information will be incorporated into existing rows. Fill ds ; ds. Update ds ;. Add " Id", Sending Insert statements directly to the database is not a very good idea if your using a dataset. You must edit your dataset, then using your adapter, send the changes to your database. But if your approach really demands that you send direct updates to the database and update your dataset then you can always refill them.

But if your records is huge then it would not be a nice way to update your dataset. Instead you could manually reflect the changes to your dataset this way:. Ask a question. The following C code illustrates this behavior. In this scenario, the comparison is case insensitive.

The Update method retrieves rows from the table listed in the first mapping before performing an update. The Update then refreshes the row using the value of the UpdatedRowSource property.

Any additional rows returned are ignored. After any data is loaded back into the DataSet , the OnRowUpdated event is raised, allowing the user to inspect the reconciled DataSet row and any output parameters returned by the command. After a row updates successfully, the changes to that row are accepted.

When using Update , the order of execution is as follows:. The values in the DataRow are moved to the parameter values. The OnRowUpdating event is raised. If the command is set to FirstReturnedRecord , then the first returned result is placed in the DataRow. If there are output parameters, they are placed in the DataRow.

The OnRowUpdated event is raised. AcceptChanges is called. Each command associated with the DbDataAdapter usually has a parameters collection associated with it. Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a.

NET Framework data provider's Parameter class. SourceColumn refers to a DataTable column that the DbDataAdapter references to obtain parameter values for the current row. SourceColumn refers to the unmapped column name before any table mappings have been applied. If SourceColumn refers to a nonexistent column, the action taken depends on one of the following MissingMappingAction values.

An exception is generated if it refers to a nonexistent column. The SourceVersion property of a. NET Framework data provider's Parameter class determines whether to use the Original, Current, or Proposed version of the column value.



0コメント

  • 1000 / 1000