Updating database with DataRow

W

wayne

I'd appreciate feedback on whether it is possible to update a database
using a DataRow object rather than a DataTable.

Suppose I have the following code:

OleDbConnection conn = new OleDbConnection(m_Dsn);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.UpdateCommand = new OleDbCommand("sp_UpdatePageItem", conn);
// ....
adapter.Update( dtMyDataTable );

This is okay providing I have a reference to whole DataTable to update.
Suppose I only have a reference to a single DataRow object which has
been modified and needs to have it's changes sent to the database. The
following works:

adapter.Update( drMyDataRow.Table );

but it will also cause other modified rows to be updates. I only want
the DataRow I jave a reference to have it's changes sent.

Am I right in thinking the only way is to use:

adapter.Update( new DataRow[] { drMyDataRow} );

Comments appreciated :)

Wayne.
 
C

Cor Ligthert

Wayne,

Only the rows with a datarow set to changed (whatever it is) will be
updated.
adapter.Update( drMyDataRow.Table );

This is the complete Table again.

And you can only update a table using the dataadapter.

I think that the way can be is to create a new datatable using something as

\\\
DataTable dtnew = new DataTable();
dtNew = oldDt.clone();
dtNew.ImportRow(drMyDataRow);
Try
{
adapter.Update(dtNew);
drMyDataRow.AcceptChanges();
}
Catch
{
whatever errorhandling
}
///
Not tested just written in this message.

I hope this helps,

Cor
 
D

David Sceppa

Wayne,

You're correct. That's the only way to submit the pending change from
a single DataRow.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2005 Microsoft Corporation. All rights reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top