DataTable.GetChanges() followed by OlDbDataAdapter.Update()

  • Thread starter Thread starter fermisoft
  • Start date Start date
F

fermisoft

How to set the command text for the following update operation?

DataTable dataTable = (DataTable) grid2Stock.DataSource;
dataTable = dataTable.GetChanges();
if (dataTable!=null)
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.UpdateCommand = new OleDbCommand();

conn.Open();

adapter.UpdateCommand.Connection = conn;

/************* How to set this update command text????
******************/
adapter.UpdateCommand.CommandText = "";

if (conn.State==ConnectionState.Open)
{
adapter.Update(dataTable);
}
}
 
How to set the command text for the following update operation?

DataTable dataTable = (DataTable) grid2Stock.DataSource;
dataTable = dataTable.GetChanges();
if (dataTable!=null)
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.UpdateCommand = new OleDbCommand();

conn.Open();

adapter.UpdateCommand.Connection = conn;

/************* How to set this update command text????
******************/
adapter.UpdateCommand.CommandText = "";

It depends on your database.
 
Fermisoft

In my opinion is The most simple is as long as you don't have datarows
consisting more than 100 columns and not have retrieved the datatable with a
join to use the commandbuilder.

The only thing you need than is to build the commands before you are using
the adapter update

OleDbCommanBuilder cmb = New OleDbCommandBuilder(adapter)
(a reference is than set in the adapter to this commandbuilder)

I would not use all those not needed expressions as "if conn.state==
ConnectionState.Open"
(In fact don't you need to open the connection with an adapter, it does it
himself as long as this is not done and closes it than in this case
properly)

I will do the update in a Try, Catch, Finally block, however. Or by Using
(What I doubt, because I assume that you want to trap update errors as
concurrency)

I hope this helps,

Cor
 
One way to avoid the command builder is write an tool which use command
builder internally to generate update command, then paste its text to
your app. I often do this tip with VS.NET: drag the table to a form at
design time, copy the VS-generated code for update/delete/insert
commands, then undo to get the form back to its original state if
neccesary.
 
Back
Top