Returning datatable using entity framework

后端 未结 5 1509
南方客
南方客 2020-12-09 10:37

I am using entity framework. There is one particular situation in my application where I have to use a stored procedure. Since there are a lot of SQL statements written in t

5条回答
  •  抹茶落季
    2020-12-09 11:10

    Thanks a lot guys. I solved it. Here is the solution:

    using (var context = new DataBaseContext())
    {
        var dt = new DataTable();
        var conn = context.Database.Connection;
        var connectionState = conn.State;
        try
        {
            if (connectionState != ConnectionState.Open) conn.Open();
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = "GetAvailableItems";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("jobCardId", 100525));
                using (var reader = cmd.ExecuteReader())
                {
                    dt.Load(reader);
                }
            }
        }
        catch (Exception ex)
        {
            // error handling
            throw;
        }
        finally
        {
            if (connectionState != ConnectionState.Closed) conn.Close();
        }
        return dt;
    }
    

提交回复
热议问题