Answer the question
In order to leave comments, you need to log in
How to populate a DataSet from a stored procedure in Entity Framework?
I have several stored procedures defined in my database. I can get data from stored procedures that only return one table:
using (var context = new UmkCmsContext())
{
var dt = new DataTable();
ProductList = new List<ProductListNode>();
#region ProcedureData
var conn = context.Database.Connection;
var connectionState = conn.State;
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "GetProductsInCategory";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@host", _httpContext.Request.Url.Host));
cmd.Parameters.Add(new SqlParameter("@catalogIdentifer", catalogIdentifer));
cmd.Parameters.Add(new SqlParameter("@categoryIdentifer", categoryIdentifer));
using (var reader = cmd.ExecuteReader())
{
dt.Load(reader);
}
}
#endregion
}
var connStr = context.Database.Connection.ConnectionString;
using (var context = new UmkCmsContext())
{
var dsMain = new DataSet();
using (var conn = new SqlConnection(connStr))
{
var sqlComm = new SqlCommand("GetProductViewModel", conn);
sqlComm.Parameters.AddWithValue("@host", Request.Url.Host);
sqlComm.Parameters.AddWithValue("@productIdentifer", productIdentifer);
sqlComm.CommandType = CommandType.StoredProcedure;
var da = new SqlDataAdapter { SelectCommand = sqlComm };
da.Fill(dsMain);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question