最佳答案
我的问题是如何使用 C # 中的 SqlDataReader
获得查询返回的行数。我已经看到了一些关于这个问题的答案,但是没有一个答案是明确定义的,除了一个用 Read()
方法执行 while 循环并增加计数器的状态。
我的问题是,我试图填充一个多维数组,第一行是列标题名,之后的每一行是行数据。
我知道我可以直接把数据转储到 List 控件中,不用担心这个问题,但是为了我自己的启发,我还想把数据从数组中拉进拉出,并以不同的格式显示它。
所以我认为我不能先打开 Read()
,然后再增加 + + ,因为这意味着我必须先打开 Read()
,然后再打开 Read()
,以获得大量的行和列数据。
举个小例子:
int counter = 0;
while (sqlRead.Read())
{
//get rows
counter++
}
然后用 for 循环遍历列并弹出
something.Read();
int dbFields = sqlRead.FieldCount;
for (int i = 0; i < dbFields; i++)
{
// do stuff to array
}