如何访问datareader中的特定行? [英] How to access specific row in datareader ?

查看:105
本文介绍了如何访问datareader中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下载批量报告时,我的内存超出了例外情况。所以我通过使用DATAREADER而不是DATASET来改变代码。 如何在DataReader中实现以下代码因为无助于使用任何数据表&我编码的数据集。



我的尝试:



< pre lang =c#> if (dataSet.Tables [ 0 ]。行[i + 1 ] [ Sr No]。ToString ()== dataSet.Tables [ 0 ]。行[i] [ Sr No]。ToString())

解决方案

DataReader只能工作通过向前读取数据集:您无法指定随机访问值以跳过到特定行。

访问DataReader中的行的唯一方法是使用循环读取每一行直到达到所需行的位置。但是......一旦你经过它们,你就不能向前向后前进。

如果你在一次读取所有行时遇到问题,那么考虑分页号码您读入DataAdapter的行,或仅为您感兴趣的行发出单独的读取请求。


DataReader没有随机访问权限。你需要记住前一行的值。

  string  previous = ; 
string current = null ;
bool isFirst = true ;

while (datareader.Read())
{
current = datareader [ Sr No]。ToString();

if (isFirst)
{
// 跳过第一行,因为我们没有任何东西可以将它与进行比较。
isFirst = false ;
}
else if (current == previous)
{
// 在这里做你的事
}

previous = current;
}


Hi , I got Memory out Of Exception while download bulk reports . So i alter the code by using DATAREADER instead of DATASET . How to achieve below code in DataReader because am helpless to use any datatables & dataset in my coding.

What I have tried:

if (dataSet.Tables[0].Rows[i + 1]["Sr No"].ToString() == dataSet.Tables[0].Rows[i]["Sr No"].ToString())

解决方案

A DataReader only works by reading forward through the data set: you can't specify a random access value to "skip" to a specific row.
The only way to access a row in a DataReader is to use a loop to read each row up to the point where you reach the row you want. But...you can't go "backward" to earlier rows once you have gone past them.
If you are having problems reading all the rows at once, then consider either "paging" the number of rows you read into a DataAdapter, or issue individual read requests for just the rows you are interested in.


DataReader has no random access. You need to "remember" the value from previous row.

string previous = null;
string current = null;
bool isFirst = true;

while (datareader.Read())
{
    current = datareader["Sr No"].ToString();
    
    if (isFirst)
    {
        // skip the first row because we don't have anything to compare it to
        isFirst = false;
    }
    else if(current == previous)
    {
        // do your thing here
    }
    
    previous = current;
}


这篇关于如何访问datareader中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆