Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple recordsets from stored proc in C#

I am having to convert an ASP classic system to C#

I have a stored procedure that can return up to 7 recordsets (depending on the parameters passed in).

I need to know how I can simply return all the recordsets as individual DataTables so that I can loop through whatever is there, skipping to the next DataTable when I get to the end of it without having to run multiple SQL statements and use multiple adapter.Fill statements to add each table into a DataSet.

In classic it was a simple Do While not objRS.EOF loop with a objRS.NextRecordset() when I got to the end of the loop to move to the next statement.

Is there anything I can use that doesn't require a total rewrite of the current back end code?

Each recordset has a different number of columns and rows. They are unrelated to each other. We return multiple recordsets from Stored Proc's to reduce traffic.

Examples would be nice.

Thanks

like image 304
user2334626 Avatar asked Aug 29 '13 12:08

user2334626


People also ask

Can we return multiple tables from stored procedure?

Yes, It is possible.

How can I get multiple result sets from a stored procedure in Entity Framework Core?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.

What can be used to return multiple result sets?

A procedure can return more than one result set to the calling environment. By default, Interactive SQL does not show multiple result sets. To enable multiple result set functionality, you can use the Options window in Interactive SQL, or you can execute a SQL statement to set the isql_show_multiple_result_sets option.

How do I return a stored procedure list in SQL Server?

In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.


2 Answers

SqlConnection con=new SqlConnection("YourConnection String");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds = new DataSet();
cmd = new SqlCommand("name of your Stored Procedure", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@SuperID", id);//if you have parameters.
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();

After this you can take advantage of different (7) recordsets using

ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]
like image 120
Sasidharan Avatar answered Oct 16 '22 13:10

Sasidharan


this will return you all you need

using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "yoursp";
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;

        conn.Open();

        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adapter.Fill(ds);

        conn.Close();
    }
}
like image 24
Ehsan Avatar answered Oct 16 '22 12:10

Ehsan