ASP MVC - how to separate tables (received from stored procedure) using table names

asp.net-mvc c# dapper

Question

I used to get multiple tables (in my old aspx project) returned from a single stored procedure, by getting the dataset into a DataSet ds, like

DataTable dt1 = ds.Tables["tabe_name1"]; DataTable dt2 = ds.Tables["tabe_name2"]

I can get the table using a model if only one table is returned, using function

con.Query<ModelName>("STORED_PROCEDURE", param, commandType: CommandType.StoredProcedure);

How can I do this in MVC using Dapper ?

Thanks :)

Accepted Answer

To get multiple grids you must use:

using(var multi = conn.QueryMultiple(...)) {
    // see below
}

Inside the "see below", you can access each grid in turn, once only - they are not named. For example:

using(...) {
    var orders = multi.Read<Order>().AsList();
    var custs = multi.Read<Customer>().AsList();
}

Popular Answer

@Marc, these also worked for me while I tried your answer.

var orders = multi.Read<Order>().Single<Order>(); var orders = multi.Read<Order>().ToList<Order>(); var orders = multi.Read<string>().Single<string>(); //this third one worked as if calling ExecuteScalar in aspx projects, ie., to get value of cell (0,0) only.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why