473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to know which rows are modified in a database

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
4 1051
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
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mario T. Lanza | last post by:
I am developing an ASP class that allows users to view/edit many records in a table directly via their browser. In order to reduce the number of SQL statements being executed against the database...
0
by: Kevin Gale | last post by:
Hi. I need to replicate data (approx. 10,000 records) from a mySQL database into a different (non mySQl) database automatically on a regular basis. I have no control over the mySQL server (apart...
4
by: Support | last post by:
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...
1
by: Yama | last post by:
Hi, I am really confused. I have created a strong typed dataset for Northwind database Customer table. Now I am loading it with a stream of XML (ADO style) with the following: Customers _cust...
6
by: Fan Ruo Xin | last post by:
I try to copy a table from production system (DB2 UDB EEE V7.2 + fixpak5) to the testing system (DB2 UDB V8.1 + fixpak4a). I moved the data from productions system by using the following steps:...
3
by: Nathan | last post by:
Hi, When I update my database, I call the updates like this (in order to submit hierarchial changes properly): daAdapter.Update(tblMyTable.Select("", "", DataViewRowState.Added))...
1
by: Jeff Silverman | last post by:
I have a PHP program that almost works. I'm running it from the command line and simulating a form using a GET method. That part is working, but I get spurious records with all of the fields...
7
by: jb1 | last post by:
Hello All, I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and...
14
by: Yas | last post by:
Hello, I have 2 tables, Table1 and Table2. I have copied all data from Table1 to Table2. However Table1 is dynamic it has new rows added and some old rows modified everyday or every other...
2
by: jogisarge | last post by:
Hi @all, ich have a datatable added to a dataset. the dataset is binded to a datagridview. How can i get all modified rows from the dataset ? I have to check all the modified rows in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.