How to execute sql queries command and retrieve its value?

How to execute sql queries command and retrieve its value?

Postby 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()"
navirius
 
Posts: 16
Joined: Sat 29 Jun 2013 11:50

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

Postby 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.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17


Return to LinqConnect (LINQ to SQL support)