如何将DataReader值转换为整数变量 [英] How to get datareader values into integer variable

查看:82
本文介绍了如何将DataReader值转换为整数变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有来自数据库的datareader值是dr = 10,30,0,12,0,42,....
我无法计算总数,即代码如下:

Hi,
i have the datareader values are dr= 10,30,0,12,0,42,... from database.
i am not able to calculate the total count i.e the code as follows:

while (dr.Read())
                {
                    int Count = 0;
                    Count = Count + int.Parse(dr["TotalCount"].ToString());
                }



执行此操作时,出现类似以下错误:
索引OutOfRange异常.

请给我一个有效的解决方案.

谢谢与问候,
Raghu.



while executing this i am getting an error like:
Index OutOfRange exception.

please give me a valid solution.

Thanks&Regards,
Raghu.

推荐答案

好吧,每次重置Count时,您都不会得到像这样的总和,假设您正确地清空了DataTable,则可以执行以下操作:
Well you wont get any sum like that as you are resetting the Count each time, assumiung that you have raead out the DataTable correctly you could do this:
                    int Count = 0;
while (dr.Read())
                {

                    Count = Count + int.Parse(dr["TotalCount"].ToString());
                }



您还应该检查是否可以使用TryParse insted将字符串解析为整数..



You should also check that you could Parse the string as an integer, using TryParse insted..


这篇关于如何将DataReader值转换为整数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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