tags:

views:

102

answers:

3

Is there a way to use simple sql queries on ASP MVC without using LINQ thing?

any link is welcome :)

+8  A: 

Of course, you can always drop down to use regular ol' ADO.NET :-)

The Data Access Application Block is also commonly used to simplify the execution of raw sql and stored procedures.

Joel Martinez
+2  A: 

Sure, you can embed plain ADO.NET objects within your controller's action methods or in a custom business logic library. A bit of an example. WARNING: DEMONSTRATION CODE ONLY. DO NOT ATTEMPT TO USE IN PRODUCTION SCENARIO.

public ActionResult Index()
{
    using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CNTax"].ConnectionString))
    {
        using(SqlCommand command = conn.CreateCommand())
        {
            command.CommandText = "select * from Names";
            command.CommandType = CommandType.Text;

            conn.Open();

            var table = new DataTable();
            table.load(command.ExecuteReader());

            return View("Index", table);
        }
    }
}

A simple code snippet to select all names from the database and return the index view with the model set as a DataTable.

Don't do this. Keep your controllers light, and create a Data Access Layer.
David P
You will most definitely not want to add this to your controller...just for the sake of demonstration and brevity.
Don't demonstrate bad practice - people will follow it.
John Saunders
@John - I added a pleasant warning that should fend off all potential users.
+1  A: 

You sure can. And it is done the same way as you normally would in an ASP.NET application just like the other answers here have indicated.

....HOWEVER, I prefer to use a tool to generate my data access layer. Some of the top choices right now are nHibernate, and LLBLGen Pro, or even Microsoft's Entity Framework

I personally would go with nHibernate or LLBLGen Pro, depending on if you want to have your data access layer driven by "domain driven design" (nHibernate) or "data driven design" (LLBLGen Pro)

Roberto Sebestyen