13

ASP クラシック システムを C# に変換する必要があります。

最大 7 つのレコードセットを返すことができるストアド プロシージャがあります (渡されたパラメーターによって異なります)。

複数の SQL ステートメントを実行したり、複数のアダプターを使用したりすることなく、すべてのレコードセットを単純に個々の DataTable として返す方法を知る必要があります。ステートメントを入力して、各テーブルを DataSet に追加します。

クラシックでは、次のステートメントに移動するためにループの最後に到達したときに、objRS.NextRecordset() を使用した単純な Do While not objRS.EOF ループでした。

現在のバックエンド コードを完全に書き直す必要がなく、使用できるものはありますか?

各レコードセットには、異なる数の列と行があります。それらは互いに無関係です。トラフィックを減らすために、ストアド プロシージャから複数のレコードセットを返します。

例はいいでしょう。

ありがとう

4

4 に答える 4

17
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]
于 2013-08-29T12:45:37.190 に答える
10

メソッドを使用して DataSet を埋める場合SqlDataAdapter.Fill()、ストアド プロシージャから返された各レコードセットは、データセット内の 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;
        }
    }
}
于 2013-08-29T12:31:03.853 に答える