Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the SQL server clock in a LINQ query

I'm writing an app which has a "check in" facility - essentially a system tray app that checks in every 20 seconds by writing to a SQL db.

I then have a second app that checks this table to see if the client has checked in, and performs an action if the client has not checked in for 60 seconds.

I need to ensure that the time written to the sql database is the local server time, not the client time - as otherwise I'll have synchronisation issues.

I'm using Linq-to-SQL - how can I acheive this?

like image 705
Ben Avatar asked Dec 09 '22 05:12

Ben


2 Answers

Actually as a general rule of thumb you should not use the local time of the server either - use UTC time (Coordinated Universal Time) instead.

In Linq to Sql you can use SqlFunctions.GetUtcDate() for this which maps to GETUTCDATE() in SQL Server.

like image 59
BrokenGlass Avatar answered Dec 26 '22 14:12

BrokenGlass


Write a user defined function that returns GetDate() and add it to you dbml file

like image 25
Magnus Avatar answered Dec 26 '22 15:12

Magnus