Get output parameter value of a stored procedure using EF Core?

后端 未结 4 1585
终归单人心
终归单人心 2020-12-21 01:12

I am using Asp.net core and EF core in my application. Basically I want to get multiple result set from a single stored procedure. Tried to search it for last 2 days no such

4条回答
  •  独厮守ぢ
    2020-12-21 01:27

    This should work. This time I filled only a DataTable, but you can fill a DataSet with multiples DataTables

    using (SqlConnection connection  = new SqlConnection(_customerContext.Database.Connection.ConnectionString))
                    {
                        SqlCommand cmd = new SqlCommand("dbo.usp_CustomerAll_sel", connection);
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        cmd.Parameters.Add(new SqlParameter("@SomeOutput", SqlDbType.BigInt) { Direction = ParameterDirection.Output, Value = -1 });
    
                        if (cmd.Connection.State != ConnectionState.Open)
                        {
                            cmd.Connection.Open();
                        }
    
    
                        connection.Open();
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
    
                        long SomeOutput = (long)cmd.Parameters["@SomeOutput"].Value;
    
                        connection.Close();
                    }
    

    Since you can't use SqlDataAdapter in .net core, you can use a third party library to archieve the same result, like NReco.Data , actually, the code is pretty similar.

提交回复
热议问题