Manually deal with SQL Server timestamp datatype (1 Viewer)

darbid

Registered User.
Local time
Today, 03:28
Joined
Jun 26, 2008
Messages
1,428
I have been doing a little bit of reading of the timestamp datatype. I know it is not a time stamp (holding some date / time) :), as far as I understand when a field of a row is updated so to is this timestamp field. I also know that my Access Front end deals with all this for me.

But my question is how can I if I wanted to, deal with this timestamp field.

Could someone please run me through or link me to a tutorial/example (vba/vb) (would prefer to not have to read c# but if that is all you have then ok) which gets this field, stores it, and then at a later stage checks it with the row again.

Thanks guys.
 

pilsdumps

Registered User.
Local time
Today, 02:28
Joined
Nov 17, 2009
Messages
31
Its used for checking data concurrency. I don't have a sample to send, but I use it like this:

- use a stored proc to get the row data including the timestamp and save this in the front end app (I use WPF)
- a user views the data, updates it and saves back to the db with an update stored proc which first checks that the timestamp is the same. If its not, then the row has been updated elsewhere whilst it was held in memory and therefore a straight update would overwrite these changes
- if a change has been found, give the user an option of what to do ie. carry on and overwrite the other changes, or my preferred way is to refresh from the db and force the user to enter their data again
 

darbid

Registered User.
Local time
Today, 03:28
Joined
Jun 26, 2008
Messages
1,428
Its used for checking data concurrency. I don't have a sample to send, but I use it like this:

- use a stored proc to get the row data including the timestamp and save this in the front end app (I use WPF)
I have played around with WPF and VB, it is kinda cool. So in your front end what type of object do you save your timestamp?

Are you sending xml/json to your back end or what is your communication type?

In your back end when you receive the info from your WPF front end what do you do with it? How do you compare it?

If you were working in C# that is ok, I will work it out.
 

pilsdumps

Registered User.
Local time
Today, 02:28
Joined
Nov 17, 2009
Messages
31
Use the MVVM design pattern to help keep your concerns separated. If you're not familiar with MVVM and are pursuing WPF I strongly suggest you adopt this pattern as it will make your life a whole lot easier - start here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

In my design, each data object (trialsite, patient, etc) is represented by a model object that inherits from a modelbase which has a RowVersion property which is a nullable Int64 (Int64?). The relational database hierarchy is represented by a TreeView control, that consists of ViewModel objects which wrap the models. A treeview is nice because you can represent the structure of the db easily (ie. a trial site can have a number of patients) and you can load the child records on demand. Each object type has a corresponding repository that handles data access (ie. PatientRepository) where the CRUD methods reside. I use a RepositoryBase to manage this, with a stored proc representing each operation (ie. select, update, delete, insert) and methods to call each that takes the specific stored proc name as a parameter (so you only need one reusable insert method, one reusable update method and so on). So, a TrialSiteRepository contains a collection of models for each trialsite, a PatientRepository contains a collection of models for each patient and so on. In my update and delete procs I use the row_version (timestamp) column to check that the loaded record is current - i.e. pass it the stored value in the model in the repository and compare it to the one in the database table for the particular record in question. If its the same my model copy is current, if not fail the update and handle it.
eg.
Code:
  if not exists(select row_version from temp__baseline_other_data where temp__baseline_other_data_pk = @pk and convert(bigint, row_version) = @row_version)
    return -20
 

darbid

Registered User.
Local time
Today, 03:28
Joined
Jun 26, 2008
Messages
1,428
Thank you. I swear you are talking a different language so I am going to have to do some reasearch on all your terms and words etc. Thanks again.
 

Users who are viewing this thread

Top Bottom