OleDbDataAdapter question

S

Son Ha

I want to copy some record from a Access database to another Access DB. My
code as follow but not working. The destAdapter.Update() return 0 record
affected.
Tell me what's wrong in my code?

public void CopyDataSet(int start, int end)
{
OleDbDataAdapter sourceAdapter = new OleDbDataAdapter("SELECT * FROM [nguoi
su dung dat] " +
"WHERE ID > " + (start - 1).ToString() +
" AND [nguoi su dung dat].id < " + (end + 1).ToString(), connectionString
+ sourceMDB);
DataSet sourceDataSet = new DataSet("nguoi su dung dat");
sourceAdapter.Fill(sourceDataSet);

OleDbDataAdapter destAdapter = new OleDbDataAdapter("SELECT * FROM [nguoi
su dung dat]",
connectionString + destinationMDB);
OleDbCommandBuilder builder = new OleDbCommandBuilder(destAdapter);
DataSet destDataSet = sourceDataSet.Clone();
destDataSet.Merge(sourceDataSet); // Same as sourceDataSet
destAdapter.Update(destDataSet); // Nothing updated :(
}

Thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I would rather use a DataReader to read the records and at the same time
inserting these records in the second DB.

Cheers,
 
B

Bart Mermuys

Hi,
[inline]

Son Ha said:
I want to copy some record from a Access database to another Access DB. My
code as follow but not working. The destAdapter.Update() return 0 record
affected.
Tell me what's wrong in my code?

The problem is that after a normal Fill operation all rowstates will be
"unmodified", merging it with the dest dataset doesn't change their state,
so when you update nothing happens because all rows are marked as
"unmodified".

So you have to make sure that the rows have an "added" rowstate :

1) Before you call "sourceAdapter.Fill(sourceDataSet)", you first set
sourceAdapter.AcceptChangesDuringFill = false;

--- OR ---

2) Manually insert each row using BeginLoadData, LoadDataRow & EndLoadData
something like:

destTable.BeginLoadData();
foreach ( DataRow dr in sourceTable.Rows )
destTable.LoadDataRow( dr.ItemArray, false );
destTable.EndLoadData();


HTH,
Greetings

public void CopyDataSet(int start, int end)
{
OleDbDataAdapter sourceAdapter = new OleDbDataAdapter("SELECT * FROM
[nguoi su dung dat] " +
"WHERE ID > " + (start - 1).ToString() +
" AND [nguoi su dung dat].id < " + (end + 1).ToString(), connectionString
+ sourceMDB);
DataSet sourceDataSet = new DataSet("nguoi su dung dat");
sourceAdapter.Fill(sourceDataSet);

OleDbDataAdapter destAdapter = new OleDbDataAdapter("SELECT * FROM [nguoi
su dung dat]",
connectionString + destinationMDB);
OleDbCommandBuilder builder = new OleDbCommandBuilder(destAdapter);
DataSet destDataSet = sourceDataSet.Clone();
destDataSet.Merge(sourceDataSet); // Same as sourceDataSet
destAdapter.Update(destDataSet); // Nothing updated :(
}

Thanks
 

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