Call a stored procedure from MVC using LINQ without specifying a class structure (since its axis is with dtynamic)

Is there a way to call a stored procedure with a dynamic set of results (without columns) without knowing the exact class model and defining it in advance. Just want to get a dynamic list and then loop through the lines?

As far as I know, I can not find any examples and I'm not sure that this is even possible, but it would be neat if I could do this ...

    var query = _db.Database.SqlQuery<????>("EXEC [dbo].[PivotOnMeterReadView]");
    var result = query.ToList();
+4
source share
1 answer

As far as I know, it is not possible to execute a stored procedure through LINQ. You will need to use SqlConnectionand SqlCommand. Below is the code snippet that I used to successfully execute the SP. Hope I can help :)

private readonly string sqlConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlConnectionString); //sqlConnectionString is the database connection.
sqlconn.Open();
using (SqlCommand cmd = new SqlCommand("InsertUsers", sqlconn)) // "InsertUsers" is the name of SP.
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@User", SqlDbType.Structured).Value = User; //Here I am passing the "User" variable as an argument to the "@User" paramter
    cmd.ExecuteNonQuery();
}
sqlconn.Close();
0

All Articles