Question

What is the best practice for executing a generic, table-less SQL query within an MVC architecture?

I'm trying to use commands such as these:

SELECT getdate();
SELECT user;

Update: This is actually for part of another project that co-exists alongside the MVC project. All of my tables so far are mapped using edmx from an existing database.

I'd like to use the date from the database in some internal calculations, rather than using the date from the server where the application/site are installed. My formulas go down to the milliseconds, and the time difference between the two servers involved throws off the math.

Was it helpful?

Solution

Implement them via Dapper:

using(SqlConnection connection = new SqlConnection("myConnectionString"))
{
    DateTime getDate = connection.Query<DateTime>("select getdate()").Single();
}

Query is an extension method that accepts a Type to map to and Single() gives you a scalar value.

Dapper is the lightweight Orm by the guys on StackOverflow: http://code.google.com/p/dapper-dot-net/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top