var data = Database.Open("databasename").Query("SELECT * FROM table); visual studio

I know you can do this in web matrix and it works fine

var data = Database.Open("databasename").Query("SELECT * FROM table); visual studio

but how can you achieve something similar in visual studio 2010 using MVC3???

I need to be able to loop through table rows using a foreach() and implement them through an api, I originally started the project in web matrix, but was asked to use mvc in visual studios instead, i have limited knowledge with it. I am using Microsoft sql server.

Also if anyone thinks I might be going the complete wrong way about achieving this, any input to point me in the right direction would be appreciated.

Upvotes: 1

Views: 1227

Answers (1)

Polynomial
Polynomial

Reputation: 3706

I've found that the ORM which most closely resembles WebMatrix is Dapper.

https://github.com/StackExchange/dapper-dot-net

It can be used with dynamic objects in a very similar manner to WebMatrix, but can also be used with strongly typed objects if you wish. It's available as a NuGet package.

The code in your question would end up looking something like the following in Dapper:

using (var con = new SqlConnection(WebConfigurationManager.ConnectionStrings["databasename"].ConnectionString))
{
    var data = con.Query("SELECT * FROM table");

    foreach (var row in data)
        // do stuff
}

Upvotes: 1

Related Questions