By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

How to know which rows are modified in a database

P: n/a
Hi,
I want to know if I have changed a few records in my database using update /
insert / delete methods, how can i later know which rows have been changed
or modified ?
I know the ExecuteNonQuery method which can give me the "number" of modified
rows, but which rows are changed how do I know ?
Any ideas ?

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On a datarow object you have the RowState property which will tell you
whether or not a row has been modified/added/deleted/etc..

If you want to see if a database has been modfied itself since you last
touched it, look into using Concurrency.

-CJ

"Support" <an*******@discussions.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
I want to know if I have changed a few records in my database using update / insert / delete methods, how can i later know which rows have been changed
or modified ?
I know the ExecuteNonQuery method which can give me the "number" of modified rows, but which rows are changed how do I know ?
Any ideas ?

Nov 21 '05 #2

P: n/a
Hi,

In addition to what CJ said about each DataRow having a RowState, you could
also create a DataFilter object and use that to filter the rows. Something
like this:

Dim myDataView as New DataView(myDataTable)
myDataView.RowStateFilter = DataViewRowState.ModifiedCurrent

If you'd rather have the original values of the changed rows, substitute
"ModifiedCurrent" with "ModifiedOriginal".

HTH,
Derrick

"Support" <an*******@discussions.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
I want to know if I have changed a few records in my database using update / insert / delete methods, how can i later know which rows have been changed
or modified ?
I know the ExecuteNonQuery method which can give me the "number" of modified rows, but which rows are changed how do I know ?
Any ideas ?

Nov 21 '05 #3

P: n/a
Not as easy as it sounds. If you are passing in statements like:
Update Orders Set TotalTax = 1000 Where ShippingState = 'TX'

And then you want to know what rows you changed... that's definitely not
simple.

A couple of ideas:
1) Triggers: this is probably the most common. Triggers in SQL Server get a
rowset that contains the changed rows. You can add a new column to your
table, and then you can add code into an Insert or Update trigger to change
the value of that column to indicate a particular value... then you can
readily look for the list of rows with that value. (Getting that value into
the trigger is hard, but this requires few changes to your existing
queries).

2) Similar to #1 above, but do this in the SQL stmt itself... Add a
transactionId column to your table, then change your SQL queries to
something like this:
Guid TransId = Guid.NewGuid();
String sSql;

sSql = "Update Orders Set TotalTax = 1000, TransactionId = " +
TransId.ToString() + " Where ShippingState = 'TX'";

(note: I prefer stored procs... I used a SQL stmt to demonstrate the
concept, not to encourage this practice. no flames please).

3) Prequeries:
In a stored procedure, open a transaction. Then query the database to
return the primary key of every row that matches your update query. Then
perform the update and commit. In the application code, capture the list of
primary keys. You now have a rowset that you can use to determine what rows
were updated.

4) Transaction tables:
Create a transaction table of the sort:
TransactionId (PK) (either UniqueIdentifier or int Identity(1,1))
RowPrimaryKey (FK)
SelectDate (datetime)

Then, when you want to update rows in the table of interest, call a
procedure that adds rows to your transaction table for the primary key of
each row that matches your particular criteria:
Insert TransTable (TransactionId, RowPrimaryKey, SelectDate) Select
@TransId, OrderId, getdate() from Orders where ShippingState = 'TX'

As you can see, I prefer Guids over Identity columns... I've spent way too
much time trying to fix a poor design when doing a merge.
If, on the other hand, you use Identity columns, the db can create the
value for you, but you will have to have the sp return the @@Identity
variable.

Anyway, the middleware now has a transaction id. Use that to update your
rows.
Update Orders Set TotalTax = 1000 where OrderId in (Select RowPrimaryKey
from TransTable Where TransactionId = @TransId)

This one allows you to create a set of rows that met the criteria at a
particular point in time, and maintain that set over time, regardless of how
the rows themselves change. You can perform many actions on the set, and
use it for auditing and reporting, both before and after you apply the
actual change.

I don't know which of these techniques you may wish to use... Just some
suggestions.

--- Nick

"Support" <an*******@discussions.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
I want to know if I have changed a few records in my database using update / insert / delete methods, how can i later know which rows have been changed
or modified ?
I know the ExecuteNonQuery method which can give me the "number" of modified rows, but which rows are changed how do I know ?
Any ideas ?

Nov 21 '05 #4

P: n/a
>
sSql = "Update Orders Set TotalTax = 1000, TransactionId = " +
TransId.ToString() + " Where ShippingState = 'TX'";

(note: I prefer stored procs... I used a SQL stmt to demonstrate the
concept, not to encourage this practice. no flames please).

RABBLE RABBLE RABBLE! Stored Procs only! RABBLE RABBLE RABBLE!

=)

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.