简体   繁体   中英

How can i return more than one results from a single stored procedure?

I am Getting a list of selected rows from two or more tables in a single procedure. How can i return the result to process in c# code.

eg. here I get 2 set of results 在此输入图像描述

It very easy. If your sp return one more record set ,your can do this:

IDataReader rd =null;//todo:
do
{
    while (rd.Read())
    {
        ///todo:
    }
} while (rd.NextResult());

You use DataSet to get the mulitple tables from stored procedure. You need multiple select statements in the stored procedure.

using (SqlConnection conn = new SqlConnection(connection))
{
    DataSet dataset = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("YourStoredProcedure", conn);
    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    adapter.Fill(dataset);
    return dataset;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM