Dataset is an object in memory, and dataadapter is related to database connection information.
Therefore, to update a dataset in batches, you must use dataadapter.
Static PrivateDataset createcommandandupdate (
StringConnectionstring,
StringQuerystring)
{
Dataset dataset =NewDataset ();
Using(Oledbconnection connection =
NewOledbconnection (connectionstring ))
{
Connection. open ();
Oledbdataadapter adapter =
NewOledbdataadapter ();
Adapter. selectcommand =
NewOledbcommand (querystring, connection );
Oledbcommandbuilder builder =
NewOledbcommandbuilder (adapter );
Adapter. Fill (Dataset );
// Code to modify data in the dataset here.
// Without the oledbcommandbuilder, this line wowould fail.
Adapter. updatecommand = builder. getupdatecommand ();
Adapter. Update (Dataset );
}
ReturnDataset;
}
|
This mechanism also brings about a benefit. For example, if we want to retrieve data from a database and update it to another database, we can use dataset as an intermediate carrier.