1

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

any link is welcome :)

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

4 Comments

Don't do this. Keep your controllers light, and create a Data Access Layer.
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 - I added a pleasant warning that should fend off all potential users.
1

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)

Comments

0

Building off of @user102220's answer:

Set up a data access layer (just a separate class or series of classes), then call these from your controller. Apply ADO.NET as necessary.

Comments

0

If you are using Entity Framework you can use SqlQuery like this:

using(var db = new DbContext())
{
var result = db.Database.SqlQuery<your object>("Your Query",params));
}

Your object : your expected result Type (object or class Type) and

Your query and params : sql command with params you should pass to query or not

Comments

0

try this solution:

var results = DynamicCall.DynamicListFromSql(_entities, "select * from users", null).ToList();


    public static IEnumerable<dynamic> DynamicListFromSql(this DbContext db, string Sql, Dictionary<string, object> Params)
    {
        using (var cmd = db.Database.Connection.CreateCommand())
        {
            cmd.CommandText = Sql;
            if (cmd.Connection.State != ConnectionState.Open) { cmd.Connection.Open(); }

            using (var dataReader = cmd.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    var row = new ExpandoObject() as IDictionary<string, object>;
                    for (var fieldCount = 0; fieldCount < dataReader.FieldCount; fieldCount++)
                    {
                        row.Add(dataReader.GetName(fieldCount), dataReader[fieldCount]);
                    }
                    yield return row;
                }
            }
        }
    }

1 Comment

Hi, welcome to Stack Overflow. When answering a question that already has many answers, please be sure to add some additional insight into why the response you're providing is substantive and not simply echoing what's already been vetted by the original poster. This is especially important in "code-only" answers such as the one you've provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.