Make DbDataReader start reading again from the beginning of the result set

☆樱花仙子☆ 提交于 2019-12-01 13:58:45

问题


How to make dr.Read(); start reading again from the beginning if a condition is satisfied?

Something like:

SqlDataReader dr = command.ExecuteReader();
for(int i=0; dr.Read() ; i++){
    if(condition ){
        //let dr.Read() start reading from the beginning
    }
}

回答1:


You can't.

The *DataReader classes are forward-only iterators.

Instead, you can store the results in a List<T> (or a DataTable)




回答2:


The only way to restart it is to grab a new reader with ExecuteReader().




回答3:


You can do that by first closing the datareader using dr.close(); then initializing it again.

If(condition)
{
    dr.close();
    dr=command.ExecuteReader();
}

Where command is the MySqlCommand object.



来源:https://stackoverflow.com/questions/16361672/make-dbdatareader-start-reading-again-from-the-beginning-of-the-result-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!