Is there a way to get the last updated/current timestamp (or counter which causes timestamps of the table rows to increment.) of a database using LINQ or Entity Framework ?
Asked
Active
Viewed 1,638 times
1
-
1Just execute `SELECT GETDATE()` ?!?!? – marc_s Jun 13 '12 at 09:13
-
Ah! I'm not talking about the datetime datatype. I'm asking for the timestamp datatype. – SDK Jun 13 '12 at 09:19
-
`TIMESTAMP` has **nothing** to do with date and/or time - you cannot get the "current" `TIMESTAMP` since there is no such thing as a "current" `TIMESTAMP`. This is simply a consecutive numerical value that SQL Server handles - and you cannot "read out" the "current" value - that doesn't exist. SQL Server will automatically set the `TIMESTAMP` when a row is modified in any way – marc_s Jun 13 '12 at 09:20
-
that's why I mentioned last updated/current :) . Isn't there a way to get it, so I could later check table rows with it to compare for modifications??? – SDK Jun 13 '12 at 09:22
-
[See this other SO question on the same topic](http://stackoverflow.com/questions/8119386/how-to-convert-sql-servers-timestamp-column-to-datetime-format) – marc_s Jun 13 '12 at 09:22
-
You can select the `TIMESTAMP` column from a given table/row - and then compare it later - yes. But there's no global "current timestamp". A `TIMESTAMP` column is typically represented as a byte array in Linq-to-SQL or EF – marc_s Jun 13 '12 at 09:23
-
Yep. I understand it has nothing to do with time. that's why I asked for the counter or something like that, for the purpose I mentioned in the earlier comment. – SDK Jun 13 '12 at 09:25
-
I thought of syncing by first sending a timestamp to the other side and when the next sync comes, getting that timestamp and compare with the table rows. Can you tell me how can I use timestamp for this scenario ? – SDK Jun 13 '12 at 09:39
2 Answers
1
See http://www.sqlnotes.info/2012/04/13/dbts-vs-min_active_rowversion/ . To get the latest used timestamp (aka rowversion) in any committed transaction run:
select @@dbts

Simon D
- 4,150
- 5
- 39
- 47
0
In Linq-to-SQL - you can use the ExecuteQuery
method on the DataContext
:
DateTime currentDateTime =
ctx.ExecuteQuery<DateTime>("SELECT GETDATE()", new object[] {}).FirstOrDefault();
Something similar ought to be possible in Entity Framework, too (ObjectContext.ExecuteStoreQuery)

marc_s
- 732,580
- 175
- 1,330
- 1,459