473,750 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update a record in SQL Database from C#.NET

I want to update a record into a table in SQL Server 2000 database from
C#. This table is used concurently for other users too, and I want to be sure
that from the read of record to the update no other user was updated the
record.
Please tell me how can I do this.
Thank you!

Nov 16 '05 #1
3 15425
Couple of ways to do that:

-Store a last update date of the record, and make sure that has stayed the
same
-In your WHERE clause, include all the original values of the fields. If any
of them have changed since, the update should affect 0 records, so you will
know that is because something has changed

"Mihaly" <Mi****@discuss ions.microsoft. com> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
I want to update a record into a table in SQL Server 2000 database from
C#. This table is used concurently for other users too, and I want to be sure that from the read of record to the update no other user was updated the
record.
Please tell me how can I do this.
Thank you!

Nov 16 '05 #2
SQ Server supports a data type called TIMESTAMP that can be used
specifically for this purpose. You do not have to specify a value for
fields that are of this type; the database will automatically assign a
value each time a record is created or updated.

Ensure that you retrieve this column with your query, and ensure that
you include it in the WHERE clause of the update. You can use this
approach whether you are building your own UPDATE statement or if you
are using the Update() method on the DataAdapter object.

Note that you should always check the return value from the execution of
the UPDATE statement or the Update() method call to ensure that the
proper number of records have been modified. If the record was changed,
then the count will be zero; leave this out and a malformed request may
update more than one record. For that reason it is often best to wrap
these updates in a transaction so that you can roll back the attempt in
case of a detected error.

-Ken

Marina wrote:
Couple of ways to do that:

-Store a last update date of the record, and make sure that has stayed the
same
-In your WHERE clause, include all the original values of the fields. If any
of them have changed since, the update should affect 0 records, so you will
know that is because something has changed

"Mihaly" <Mi****@discuss ions.microsoft. com> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
I want to update a record into a table in SQL Server 2000 database from
C#. This table is used concurently for other users too, and I want to be


sure
that from the read of record to the update no other user was updated the
record.
Please tell me how can I do this.
Thank you!


Nov 16 '05 #3
I thinked to this solution, but I tried an other. When the user begin the
update, I begin a transaction with isolation level read committed and read
the record. Then the user change the record's date and saves the changees.
When the user saves the new values I do the update and the commit transaction.
In this case I can read the record which was not updated and I can update
the record after the first user updated it.
If I use the isolation level serializable, I get the good solution, but
if the user which was read first the record is too slow, he blocks the other
users.
The best solution is with timestamp? In this case I need a timestamp
field in any table.

Thank you for help and I wish you a Happy New Year!
Mihaly

"Ken Allen" wrote:
SQ Server supports a data type called TIMESTAMP that can be used
specifically for this purpose. You do not have to specify a value for
fields that are of this type; the database will automatically assign a
value each time a record is created or updated.

Ensure that you retrieve this column with your query, and ensure that
you include it in the WHERE clause of the update. You can use this
approach whether you are building your own UPDATE statement or if you
are using the Update() method on the DataAdapter object.

Note that you should always check the return value from the execution of
the UPDATE statement or the Update() method call to ensure that the
proper number of records have been modified. If the record was changed,
then the count will be zero; leave this out and a malformed request may
update more than one record. For that reason it is often best to wrap
these updates in a transaction so that you can roll back the attempt in
case of a detected error.

-Ken


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10554
by: Mark | last post by:
A beginner in this area, I have been able to read a record from a MySQL database and populate an HTML form (wow!). Now, my goal is to allow the user to edit the contents of the form and then update the record in MySQL. The problem is, as soon as the "Update" button (type="submit") is pressed, all of the data disappear from the form. How can that be prevented? Here is my code:
16
17017
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
3
3451
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all the necessary records in it when testing it. I get the error "No value given for one or more required parameters." when I try to update the database. Can you tell me what am I doing wrong?
2
1971
by: Agnes | last post by:
I got a table which without primary key. case 1) I can process insertcommand and daMytable.update..etc case 2) However, if i process updatecommand , As I run daMytable.update .It returns error said..."cannot process update command without .... key" I understand I didn't set primary key (is my fault).But why case 1) can process very well ???
5
3541
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I do the update the changed parentid in the child table fails to change. No error is given its just that the change is not written to the Database. When I step through the records for the child table the one I would expect to be changed has a row...
5
5606
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that uses very high volume DB transactions - in the order of tens of millions per day . . . Anyway, the current database which will remain nameless, but begins with O and rymes with debacle (sorta), has a problem with high volume work when it comes to...
2
19608
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the field in table2. What I have is a form that is linked to Table2. If the users want to change a field in the main database (table1), they fill the change in to the form (which is linked to table2) If there is no change to the field, they simply...
16
3496
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
2
3111
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i havnt tested the index yet ) so you can use an .UPDATE( dataTable ) on the data adapter. Otherwise you will get an exception error. Is this statement true? ---- Now me fumbling thru
0
9583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.