473,383 Members | 1,853 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,383 software developers and data experts.

Using SQL Server Timestamp

I have a database that I don't want to lock. I decided that before any
updates can occur I would check a timestamp value and ensure that nobody
else updated before I did (avoiding the 'last update wins' scenario).

I have a problem, I can read the Timestamp from the db when I read the
record. I currently use the data to pre-fill a form (gee go figure ;) ) and
the user changes some values and updates.

I don't know what to DO with the timestamp value while I am holding it. I
have tried putting it into a hidden value on the form but the value does not
seem to translate back and forth.

Do I have to store it as in memory as part of the session or can I somehow
convert to and from text??

I have about 0 experience working with byte arrays so the best answer is one
with an example.

Thanks.
Nov 18 '05 #1
1 7975
Hope this was what you were expecting! Here is some code from my project
that using a "timestamp" (aka rowversion)

"A future release of Microsoft® SQL ServerT may modify the behavior of the
Transact-SQL timestamp data type to align it with the behavior defined in
the standard. At that time, the current timestamp data type will be replaced
with a rowversion data type.

Microsoft® SQL ServerT 2000 introduces a rowversion synonym for the
timestamp data type. Use rowversion instead of timestamp wherever possible
in DDL statements. rowversion is subject to the behaviors of data type
synonyms."

(As far as what to do with it, that depends. I shove mine into ViewState
when the page load, and then read it again after postback)
Example of typical sproc:
CREATE PROCEDURE usp_SomeSPROC(
@MyID int,
@RowVersion rowversion OUTPUT,
<blah blah>
)
AS
SET NOCOUNT ON

BEGIN TRAN

DECLARE @ErrorSave int

DECLARE @CurrRowVersion rowversion

SELECT @CurrRowVersion = [RowVersion] FROM MyTable WHERE MyID = @MyID

IF @CurrRowVersion <> @RowVersion
BEGIN
ROLLBACK
RETURN -1
END

<do some tsql statements here>

IF @@ERROR <> 0
SET @ErrorSave = @@ERROR

SELECT @RowVersion = [RowVersion] FROM MyTable WHERE MyID = @MyID

IF @ErrorSave <> 0
ROLLBACK
ELSE
COMMIT
Back in my code, typical SQL method:
This is all psuedo code...

Public RowVersion(8) As Byte

Public Sub MyMethod(<blah blah>)
Dim cn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim cmd As SqlCommand = New SqlCommand("usp_SomeSPROC", cn)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add("@MyID", Me.MyID)
cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue

With cmd.Parameters.Add("@RowVersion", SqlDbType.Timestamp)
.Value = Me.RowVersion
.Direction = ParameterDirection.InputOutput
End With

Try
cn.Open()
cmd.ExecuteNonQuery()

If CInt(cmd.Parameters("RETURN_VALUE").Value) = -1 Then
' this row has been modified by somebody else!!!
Throw New ModifiedByAnotherException
End If

Me.RowVersion = CType(cmd.Parameters("@RowVersion").Value,
Byte())

Catch ex As SqlException
Throw New Exception("error while doing mymethod!", ex)
Finally
cn.Close()
End Try

End Sub

Public Class ModifiedByAnotherException

Inherits System.ApplicationException

Public Sub New()
MyBase.New("Timesheet was modified by another user!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Timesheet was modified by another user!",
InnerException)
End Sub

End Class

HTH,
Greg
"Roger Twomey" <ro******@vnet.on.ca> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have a database that I don't want to lock. I decided that before any
updates can occur I would check a timestamp value and ensure that nobody
else updated before I did (avoiding the 'last update wins' scenario).

I have a problem, I can read the Timestamp from the db when I read the
record. I currently use the data to pre-fill a form (gee go figure ;) )
and
the user changes some values and updates.

I don't know what to DO with the timestamp value while I am holding it. I
have tried putting it into a hidden value on the form but the value does
not
seem to translate back and forth.

Do I have to store it as in memory as part of the session or can I somehow
convert to and from text??

I have about 0 experience working with byte arrays so the best answer is
one
with an example.

Thanks.

Nov 18 '05 #2

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

Similar topics

8
by: Phoenix | last post by:
Here's a challenge that is killing me: I've got 2 web servers and a SQL Server and about 5,000 'users' who stay connected to the site all day. I have a page that is supposed to be 'real-time',...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
6
by: bryan.seaton | last post by:
I have a delete statement that is not doing what I want it to do: Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21 DAYS); It is supposed to delete all records that are 21...
6
by: Max | last post by:
Hi, I have SqlServer 2000 as back end and Access 2000 as front-end. All tables from Sqlserver are linked to Access 2000. I am having write conflict problem with one of my form which is bound to...
3
by: Fardude | last post by:
I have a few tables in SQL Server 2000 and a small Access 97 that has linked tables used to insert/update/delete into SQL Server 2k tables. Everything works fine except Updating one of these...
6
by: Greg Collins [MVP] | last post by:
For background, please refer to my original thread: http://groups.google.com/groups?selm=efe5w7tYEHA.3112%40tk2msftngp13.phx.gbl I've thought of a potential way around the issue, but I'm new to...
7
by: mybappy | last post by:
Hi: I am trying to use timestamp field of SQL Server to maintain concurrency. My problem is how do I store the timestamp value in my webform. The hidden field does not work as I get some cast...
0
by: Jean-Michel POURE | last post by:
Dear friends, I am currently testing Ulogd ip traffic logging system with PostgreSQL. It works in conjunction with GNU/Linux iptables. The Ulogd project can be found here:...
1
by: nickvans | last post by:
Hello generous SQL experts! I'm new to the SQL Server environment, but am trying to get up to speed. I am trying to help a coworker create a pivot table for use with an Infragistics chart. I have...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.