3

以下のようなコードがあります。このストアド プロシージャ 'get2Rows' は 2 行を返します。最初の行を取得して列の値を取得し、次に 2 番目の行を取得して列の値を取得します。どのように繰り返す必要がありますか?

using (System.Data.Common.DbCommand dbCommand = DataAccess.Instance().Database.GetStoredProcCommand("get2Rows"))
{       
    using (IDataReader reader = DataAccess.Instance().Database.ExecuteReader(dbCommand))
    {
         while (reader.Read())//here i want to get the 1st row
         {
             //here i want to get the columns of the 1st row....
         }                            
    };                        
}
4

1 に答える 1

1
while (reader.Read())//here i want to get the 1st row
{
    //here i want to get the columns of the 1st row....
    int firstColumn = Convert.ToInt32(dr[0]["intColumn"].ToString());
    string secondColumn = dr[0]["stringColumn"].ToString();
    // etc.
}

ループしているため、両方の行を取得しています。それらをどのように保管したいかはあなた次第です。おそらくコレクションに入れますか?

詳細: http://www.csharp-station.com/Tutorial/AdoDotNet/lesson04

私のアドバイスは、DataReader を使用している場合、一度に 1 行ずつ操作することです。メモリ内のすべての行が必要な場合は、代わりに DataSet を使用する必要があります。

于 2012-05-22T16:31:27.353 に答える