开发者

sql stored procedure - Having problems retreiving multiple rows of data with Idatareader

开发者 https://www.devze.com 2023-03-12 05:32 出处:网络
I have a stored procedure that does this: SELECT TOP 4 FROM dbo.test (table contains 5 rows) My c# code is:

I have a stored procedure that does this:

SELECT TOP 4 FROM dbo.test

(table contains 5 rows)

My c# code is:

IDataReader test= ((IDataReader)(DataProvider.Instance().ExecuteReader("fetchtest")));
 test.Read();
 title1.Text = test.GetString(0);
 title2.Text = test.GetString(1);
 title3.Text = test.GetString(2);
 title4.Text = test.GetString(3); 

However I can only display 0 and 1. 2+ will give m开发者_JAVA百科e an index error. Is there a better way to retrieve all rows?

Thanks


IDataReader.GetString(2) returns the value of the third column, not record. You need to use Read() method to advance to the next record.


Like a1ex07 said, GetString gets the nth column. Here is a complete code sample.

List<string> titles = new List<string>();
using (IDataReader test = ((IDataReader)(DataProvider.Instance().ExecuteReader("fetchtest"))))
{
    while (test.Read())
    {
       titles.Add(test.GetString(0));
    }
}


title1.Text = titles[0];
title2.Text = titles[1];
title3.Text = titles[2];
title4.Text = titles[3];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号