无效的尝试访问字段的数据读取器问题 [英] Data Reader Problem for Invalid attempt to access a field

查看:62
本文介绍了无效的尝试访问字段的数据读取器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿evry1 ..

我已经开发了一个项目,其中编写了以下代码,但在第3或第4次迭代后,在while循环中出现了错误.

所以请让我知道查询是错误的还是任何其他问题.



字符串qr2 =从sampleOB中选择quan,其中日期<(从sampleob中选择max(date),日期<""+ CDate +")和(Special =(从产品代码中选择RMtype,其中RMCode ="" + RMCode + "限制1)或subparticular =''"+ RMCode +"'')限制1;


while()
{
con.Open();
cmd = new MySqlCommand(qr2,con);
dr = cmd.ExecuteReader();
dr.Read();
currBalOB = Convert.ToDouble(dr [0]);//在调用Read()之前尝试访问字段无效尝试
con.Close();
}


谢谢
swapnil

Hey evry1..

I had developed a project, in which following code is written But i got the ERROR in t while loop after 3rd or 4th iteration.

So plz let me kn whether the query is wrong or any other problem.



string qr2 = "select Quan from sampleOB where date<(select max(date) from sampleob where date<''"+ CDate +"'') and (Particular=(select RMtype from productcode where RMCode=''"+ RMCode +"'' limit 1) or subparticular=''" + RMCode + "'') limit 1";


while()
{
con.Open();
cmd = new MySqlCommand(qr2, con);
dr = cmd.ExecuteReader();
dr.Read();
currBalOB = Convert.ToDouble(dr[0]);//Invalid attempt to access a field before calling Read()
con.Close();
}


thank you
swapnil

推荐答案

您为什么这样做?
充其量,您将坐在一个循环中,从DB中读取相同的数据,并加载相同的记录,并将相同的字段转换为双精度,然后丢弃结果.

为什么?
为什么不查看读者返回的所有记录呢?
为什么不退出循环?

顺便说一句:您也应该处理MySqlCommand对象,而不是每次都创建一个新对象.
Why are you doing that?
At best, you will sit in a loop reading the same data from teh DB and loading the same record and converting the same field to a double, then throwing away the result.

Why?
Why not look at all the records the reader returns instead?
Why not have an exit from the loop?

BTW: you should Dispose your MySqlCommand objects as well, rather than just creating a new one each time.


这篇关于无效的尝试访问字段的数据读取器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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