How to execute sql queries command and retrieve its value?

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
navirius
Posts: 16
Joined: Sat 29 Jun 2013 11:50

How to execute sql queries command and retrieve its value?

Post by navirius » Fri 19 Jul 2013 08:00

I want to retrieve server datetime, how can I achieve this?
ex. in my sql "select now()" in sql server "select getdate()"

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: How to execute sql queries command and retrieve its value?

Post by MariiaI » Mon 22 Jul 2013 09:54

You could do it via the DataContext.ExecuteQuery method, for example:
1) SQL Server:

Code: Select all

var data = dt.ExecuteQuery<DateTime>("select getdate();");
2) MySQL:

Code: Select all

var data = dt.ExecuteQuery<DateTime>("select now();");
There is also another universal way:

Code: Select all

var data = dt.NonEmptyTable.Take(1).Select(t => DateTime.Now).First();
JIC: the NonEmptyTable table - any table that is available in your model, but it must necessarily be non-empty.

Post Reply