簡體   English   中英

從C#中的存儲過程返回多個記錄集

[英]Return multiple recordsets from stored proc in C#

我不得不將ASP經典系統轉換為C#

我有一個存儲過程,最多可以返回7個記錄集(取決於傳入的參數)。

我需要知道如何簡單地將所有記錄集作為單獨的DataTable返回,以便我可以遍歷那里的任何內容,當我到達它的末尾時跳到下一個DataTable而不必運行多個SQL語句並使用多個適配器。填充語句以將每個表添加到DataSet中。

在經典中,當我到達循環的末尾以移動到下一個語句時,它是一個簡單的Do While而不是objRS.EOF循環和objRS.NextRecordset()。

有什么我可以使用,不需要完全重寫當前的后端代碼?

每個記錄集具有不同數量的列和行。 他們彼此無關。 我們從Stored Proc's返回多個記錄集以減少流量。

例子很好。

謝謝

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();

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

ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]

如果使用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
}

如果您使用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;
        }
    }
}

這將為您提供所需的一切

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();
    }
}

你可以通過使用if (dr.NextResult())檢查dr是否還有任何記錄集

然后使用dr.read再次循環

試試這個

string connStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);

try
{
    string str1 = "select productid,productname from products;select VendorFName from vendor";
    SqlCommand com = new SqlCommand(str1, Con);

    com.Connection.Open();

    SqlDataReader dr = com.ExecuteReader();

    DropDownList1.Items.Add("Select Product Id");
    DropDownList2.Items.Add("Select Vendor Name");

    while(dr.Read())
    {
        DropDownList1.Items.Add(dr.GetValue(0).ToString());
    }

    if (dr.NextResult())
    {
        while (dr.Read())
        {
            DropDownList2.Items.Add(dr.GetValue(0).ToString());
        }
    }
}
catch (Exception ex)
{
}
finally
{
    if (Con.State == ConnectionState.Open)
    {
        Con.Close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM