S
S
saxer2016-03-23 21:37:57
ASP.NET
saxer, 2016-03-23 21:37:57

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
}

But if the stored procedure returns multiple tables, I'm in trouble. On the local server, I can get them like this:
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);
                }
}

But when publishing a site on a hosting (Azure), this method does not work. The ConnectionString does not contain a password.
It is important for me to get the data in a "raw" form (without creating an object and filling it).
Has anyone encountered such a problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question