Tracking changes in database with DataSet

  • Thread starter Thread starter Bartosz Matuszczak
  • Start date Start date
B

Bartosz Matuszczak

Hi
I need to track any changes of rows in my database. I do this by filling in
a loop dataset to have newest data from data source.
Moreover I want to know which data has been modifed in database. I refresh
dataset every second and i fill it up with datadapter. It's working but i
don't know which rows has been modified. I add second dataset to help me do
that. Please look at this short code:
....
SqlDataAdapter myAdapter= new SqlDataAdapter();
myAdapter.SelectCommand = objCommand;
DataSet myDataSet= new DataSet();
DataSet myTmpDataSet= new DataSet();
myAdapter.FillSchema(myDataSet,SchemaType.Mapped);
myAdapter.FillSchema(myTmpDataSet,SchemaType.Mapped);

while (true)
{
myAdapter.Fill(myTmpDataSet);
myDataSet.Merge(myTmpDataSet,false);
if(myDataSet.HasChanges())
{
System.Console.WriteLine("Some data has been
changed");
// myDataSet.GetChanges() should give me dataset
only with changed data in datasource
}
System.Threading.Thread.Sleep(1000);
myDataSet.AcceptChanges();
}

Merge refresh myDataSet with new values comming from data source, but
function HasChanges is always false. I know that is true when i change
dataset , not data source. Is there any solution that help me to have
"fresh" data set and to know what data has been changed in data source and
when? Changes occurs only in the database not in dataset.


Thanks Bartek
 
Hi,

I don't think that there is an automated solution.
You might loop through rows and compare them field by field.
 
So ther's no easy way to reflect actual data from data source in dataset?
Maybe ther's another way to do that without dataset.


Bartek
 
Bartosz,
Change: myDataSet.Merge(myTmpDataSet,false);
To: myDataSet.Merge(myTmpDataSet,true);

This will preserve the change information in myDataSet.

Cecil Howell MCT, MCSD

EMail: Add an 'h' at the end of cecil in return address.
 
Back
Top