从C#中的存储过程返回多个记录集

use*_*626 11 c# sql asp.net datatable dataset

我不得不将ASP经典系统转换为C#

我有一个存储过程,最多可以返回7个记录集(取决于传入的参数).

我需要知道如何简单地将所有记录集作为单独的DataTable返回,以便我可以遍历那里的任何内容,当我到达它的末尾时跳到下一个DataTable而不必运行多个SQL语句并使用多个适配器.填充语句以将每个表添加到DataSet中.

在经典中,当我到达循环的末尾移动到下一个语句时,它是一个简单的Do While而不是objRS.EOF循环和objRS.NextRecordset().

有什么我可以使用,不需要完全重写当前的后端代码?

每个记录集具有不同数量的列和行.他们彼此无关.我们从Stored Proc's返回多个记录集以减少流量.

例子很好.

谢谢

Sas*_*ran 13

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();
Run Code Online (Sandbox Code Playgroud)

在此之后,您可以使用不同的(7)记录集

ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]
Run Code Online (Sandbox Code Playgroud)


Gar*_*thD 10

如果使用SqlDataAdapter.Fill()方法填充DataSet ,则从存储过程返回的每个记录集将作为数据集中的DataTable返回

DataSet dataset = new DataSet();
using (var adapter = new SqlDataAdapter("yourStoredProcedure", yourConnectionString))
{
    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    adapter.Fill(dataset);
}
for (int i = 0; i < dataset.Tables.Count; i++)
{
    // Do something for each recordset
}
Run Code Online (Sandbox Code Playgroud)

如果您使用SqlDataReader,则使用该SqlDataReader.NextResult()方法可以使用该方法前进到下一个记录集:

using (var connection = new SqlConnection(yourConnectionString))
using (var command = new SqlCommand("yourStoredProcedure"))
{
    connection.Open();
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            // do something with first result set;
        }
        if (reader.NextResult())
        {
            while (reader.Read())
            {
                // do something with second result set;
            }
        }
        else
        {
            return;
        }
        if (reader.NextResult())
        {
            while (reader.Read())
            {
                // do something with third result set;
            }
        }
        else
        {
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上你可以,我通常更喜欢使用SqlDataReader,因为内存使用率较低,只存储当前记录,而不是打扰已经读过的内容,还有什么还没有读过.然而,在[这个答案](http://stackoverflow.com/a/1083223/1048425)中比较数据集和数据集合器有一个很好的类比. (2认同)

Ehs*_*san 6

这将为您提供所需的一切

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是我用来从存储过程中获取多个记录集的方法.只是为了澄清,记录集存储在`ds.Tables`中,您可能希望在开头放置`DataSet ds = new DataSet();`以便您可以在`using(sqlConnection conn = new)之外引用它. System.Data.SqlClient.SqlConnection(connString))`block.只是一个想法. (2认同)