简体   繁体   中英

Unable to check for changed rows in a dataset

I have a table within my program which stores information about messages and would like to detect when new information has been added to it. The reason I am doing this is beucause I would like to show the new data to the user only when there is new data instead of having to constantly get all the rows and display them.

The way I decided to do this was through the use of the dataSet.HasChanged() function which should essentially check the dataset for any new rows and a function called DataChanged returns dataSet.HasChanged() value.

However, the function I am using always returns false (even when there are changes)

Here is the function...

public bool DataChanged(string Table)
{
    //This is the variable that will be returned
    bool ChangesMade;

    //Create the  adapter
    OleDbDataAdapter adapter = new OleDbDataAdapter(Table, connector);
    //Clear the current data in the dataset
    dataSet.Clear();
    //Open the connection and fill the dataset
    connector.Open();
    adapter.Fill(dataSet, "TableData1");
    connector.Close();
    return ChangesMade = dataSet.HasChanges();
}

Changes for some reason are never detected and therefore this function always returns false even after I add a new record to the dataset.

An alternative method that provides the functionality explained in paragraph one would be very helpful and the fixing of my current method ever more so.

Here is an easy way to do this.

Make sure a Timestamp/Rowversion column exists for each table you wish to track changes to.

Return the current database Timestamp to your calling program as part of your data result set (DataSet in this case). For example, add the following query to your result set.

SELECT @@DBTS;

Use this returned value the next time you run your query with the following added to the existing WHERE clause(s) as appropriate.

...
AND (@LastRowversionValue IS NULL
    OR TableName.RowversionColumn > @LastRowversionValue)
...

Pass NULL for the @LastRowversionValue the first time the query is ran to get the process started.

Any rows that are inserted/updated into the table(s) since the last time you retrieved data, will have an Rowversion greater than the one you stored from the last execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM