IEnumerator不返回任何值 [英] IEnumerator not returning any value

查看:106
本文介绍了IEnumerator不返回任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中获取数据并使用IEnumerator检索其值,但是它不返回任何值,而用fillDataTable()方法编写的select查询成功返回了数据.最后一行不返回任何值.请帮助.
我的代码是:-

I want to fetch data from a database and retrieve its value using IEnumerator.But it does not return any value whereas the select query written inside fillDataTable() method returns data successfully.The last line is not returning any value.Please help.
My code is :-

DataTable dataTable = new DataTable();
dataTable = fillDataTable();//method used for filling datatable.
IEnumerator ienumerator = dataTable.Rows.GetEnumerator();
DataRow dataRow = (DataRow)ienumerator.Current;
string s = dataRow.ItemArray[0].ToString();


感谢和问候.


Thanks and Regards.

推荐答案

DataTable dataTable = new DataTable();
dataTable = fillDataTable();//用于填充数据表的方法.
IEnumerator ienumerator = dataTable.Rows.GetEnumerator();

//此行丢失了
ienumerator.MoveNext();


DataRow dataRow =(DataRow)enumerator.Current;
字符串s = dataRow.ItemArray [0] .ToString();


问候
Manuj Sharma
DataTable dataTable = new DataTable();
dataTable = fillDataTable();//method used for filling datatable.
IEnumerator ienumerator = dataTable.Rows.GetEnumerator();

//This line is missing
ienumerator.MoveNext();


DataRow dataRow = (DataRow)ienumerator.Current;
string s = dataRow.ItemArray[0].ToString();


Regards
Manuj Sharma


根据MSDN文档,枚举数的默认位置是在集合的第一个元素之前.因此,在创建枚举数之后和访问Current属性之前,您可能必须调用MoveNext().

但是,在移到集合的第一个元素之前调用Current应该会引发异常.据我了解,您的代码不会引发异常,所以问题可能出在其他地方...
According to the MSDN documentation the default position of an enumerator is before the first element of the collection. Therefore you might have to call MoveNext() after creating the enumerator and before accessing the Current property...

However, calling Current before you move to the first element of the collection should throw an exception. As far as I understand your code does not throw an exception, so the problem might be somewhere else...


这篇关于IEnumerator不返回任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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