简体   繁体   English

sql 存储过程 - 使用 Idatareader 检索多行数据时出现问题

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

I have a stored procedure that does this:我有一个执行此操作的存储过程:

SELECT TOP 4 FROM dbo.test

(table contains 5 rows) (表格包含 5 行)

My c# code is:我的 c# 代码是:

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 me an index error.但是我只能显示 0 和 1。2+ 会给我一个索引错误。 Is there a better way to retrieve all rows?有没有更好的方法来检索所有行?

Thanks谢谢

IDataReader.GetString(2) returns the value of the third column, not record. IDataReader.GetString(2)返回第三列的值,而不是记录。 You need to use Read() method to advance to the next record.您需要使用Read()方法前进到下一条记录。

Like a1ex07 said, GetString gets the nth column.就像 a1ex07 所说,GetString 得到第 n 列。 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM