How do I loop through rows with a data reader in C#?

后端 未结 8 575
孤街浪徒
孤街浪徒 2020-11-30 06:29

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 o

8条回答
  •  借酒劲吻你
    2020-11-30 07:04

    How do I loop through rows with a data reader in C#?

    IDataReader.Read() advances the reader to the next row in the resultset.

    while(reader.Read()){
        /* do whatever you'd like to do for each row. */
    }
    

    So, for each iteration of your loop, you'd do another loop, 0 to reader.FieldCount, and call reader.GetValue(i) for each field.

    The bigger question is what kind of structure do you want to use to hold that data?

提交回复
热议问题