我如何通过循环排在C#中的数据读取器? [英] How do I loop through rows with a data reader in C#?
问题描述
我知道我可以使用而(dr.Read()){...}
但环在我的表中的每个领域,我想要检索的所有值从第一行,然后第二...等
I know I can use while(dr.Read()){...}
but that loops every field on my table, I want to retrieve all the values from the first row, and then second... and so on.
让我们说我有一个表是这样的:
Let's say I have a table like this:
ID--------------Value1--------------Value2------------------Value3
1 hello hello2 hello3
2 hi1 hi2 hi3
首先我想要得到的,你好
, hello2
和 hello3
,然后去第二排,并得到所有的值。
first I want to get, hello
, hello2
and hello3
and then go to the second row and get all the values.
有没有办法来实现这一目标?我希望有人明白我的意思。
Is there a way to achieve this? I hope somebody understand what I mean.
我很抱歉,这是现在解决了。我只是没有合适的编码...
并耶SqlDataReader.Read()方法做的事情是应该做的,又错误是我的。
推荐答案
就是这样的的DataReader
的作品,它的设计来读取数据库行一次。
That's the way the DataReader
works, it's designed to read the database rows one at a time.
while(reader.Read())
{
var value1 = reader.GetValue(0); // On first iteration will be hello
var value2 = reader.GetValue(1); // On first iteration will be hello2
var value3 = reader.GetValue(2); // On first iteration will be hello3
}
这篇关于我如何通过循环排在C#中的数据读取器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!