Trouble updating access database with dataset

  • Thread starter Thread starter emde
  • Start date Start date
E

emde

Here is my code. It should be updating the first field in the first record
of the table. No errors occur when I run this but no data is updated either.
Any ideas?

Thanks.


System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Password=pass;User ID=username;" +
@"Jet OLEDB:System database=q:\system.mdw;" +
@"Data source=q:\db.mdb";

conn.Open();

string query = "select * from table1";

OleDbDataAdapter da = new OleDbDataAdapter( query, conn );

DataSet ds = new DataSet();

da.Fill( ds );

ds.Tables[0].Rows[0].ItemArray[0] = "newvalue";

da.Update( ds );
 
you said "no data is updated"

did you check the dataset or the database file?

if you ONLY checked the dataset
{
append ds.AcceptChanges() after da.Update(ds);
run your application;
and check the dataset again;
}
else // you checked the database file, and no data was updated
{
check the da.UpdateCommand.Text;
}
 
emde,

When you create the new data adapter, the select statement of the adapter is
automatically created for you.

To update the database you need to create the Insert, Update and Delete
commands of the data adapter.

The easiest way to do this is with the command builder object.

In VB I would add the following code after you declare the new data adpater:

<code (VB.NET) >
Dim dataCommandBuilder As New OleDb.OleDbCommandBuilder(da)
da.InsertCommand = dataCommandBuilder.GetInsertCommand
da.DeleteCommand = dataCommandBuilder.GetDeleteCommand
da.UpdateCommand = dataCommandBuilder.GetUpdateCommand
</code>

Then calling Update on the dataadapter should work.

I would also not use a full dataset, I would use a datatable instead. The
Dataset will contain many tables and relations and could cause an ambiguous
situation.



-Sam Matzen
 
Samuel L Matzen said:
emde,

When you create the new data adapter, the select statement of the adapter is
automatically created for you.

To update the database you need to create the Insert, Update and Delete
commands of the data adapter.

The easiest way to do this is with the command builder object.

In VB I would add the following code after you declare the new data adpater:

<code (VB.NET) >
Dim dataCommandBuilder As New OleDb.OleDbCommandBuilder(da)
da.InsertCommand = dataCommandBuilder.GetInsertCommand
da.DeleteCommand = dataCommandBuilder.GetDeleteCommand
da.UpdateCommand = dataCommandBuilder.GetUpdateCommand
</code>

Then calling Update on the dataadapter should work.

I would also not use a full dataset, I would use a datatable instead. The
Dataset will contain many tables and relations and could cause an ambiguous
situation.



-Sam Matzen


I am new to this and I am looking for something similar to a recordset that
is updateable. Maybe I should be looking into a datatable instead of a
dataset. I would like to loop through the data with the possibility of
updating a field.

Hmmm.
 
Back
Top