DataReader的为空或空 [英] DataReader is null or empty

查看:253
本文介绍了DataReader的为空或空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#

我有一个从MySQL数据库返回记录lsit一个DataReader。

I have a datareader that return a lsit of records from a mysql database.

我想写如果DataReader的ISNULL来检查代码。这背后的逻辑是:如果有DataReader的字段,显示信息,否则隐藏字段

I am trying to write code that checks if the datareader isnull. The logic behind this is: If the datareader having field then display the info otherwise hide the field.

我曾尝试:

cmd1 = new OdbcCommand("Select * from tb_car where vehicleno = '" + textbox2.text  + "';", dbcon);
        dr1 = cmd1.ExecuteReader();

if (dr1["tb_car"]. != DBNull.Value) 
{ 
    textbox1.Text  = "contains data"; 
} 
else 
{
    textbox1.Text = "is null"; 
}



上面的代码给了我这个错误:

The above code gives me this error:

异常详细信息:System.IndexOutOfRangeException:其他

Exception Details: System.IndexOutOfRangeException: Additional

任何帮助将不胜感激...

Any help would be greatly appreciated...

推荐答案

我在这里看到了一些问题。首先,它看起来像你想在该行访问表名称:

I see a few problems here... First, it looks like you're trying to access the table name in the line:

if(dr1["tb_car"] != DBNull.Value

您应传递一个字段名,而不是表名所以,如果表名为tb_car有一​​个名为CarId领域,你想有你的代码如下所示:

You should be passing a FIELD NAME instead of the table name. So if the table named "tb_car" had a field called CarId, you would want to have your code look like:

if(dr1["CarId"] != DBNull.Value)

如果我是正确的,那么有可能是没有名为tb_car字段,索引超出误差范围,是因为DataReader的是寻找列集合中命名的项tb_car,并没有找到它。这几乎是什么错误意味着

If I'm right, then there is probably no field named "tb_car", and the Index is Out of Range error is because the DataReader is looking for an item in the column collection named "tb_car" and not finding it. That's pretty much what the error means.

其次,之前,你甚至可以检查它,你必须调用DataReader的的Read()命令首先从数据库中读取一行。

Second, before you can even check it , you have to call the DataReader's Read() command first to read a line from the database.

所以真的是你的代码看起来应该像...

so really your code should look like...

while(dr.Read())
{
   if(dr1["CarId"] != DBNull.Value)
   {
      ....

等等。

看到这里正确使用一个DataReader的:的http:// MSDN。 microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

See here for the proper use of a DataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

最后,​​如果你只是检查,看看是否有表中的任何行,可以忽略上述所有和使用的HasRows 财产在

Finally, if you're just checking to see if there are any rows in the table, you can ignore all of the above and use the HasRows property as in

if(dr.HasRows)
{
   ....

但如果你使用如果有摆在首位行而(dr.Read())语法,在while循环中的代码将只执行的,所以HasRows可能是不必要的,如果你不想做没有结果什么。你会仍然想,如果你想返回的消息,如没有找到结果来使用它,当然..

although if you're using the while(dr.Read()) syntax, the code in the while loop will only execute if there are rows in the first place, so the HasRows could potentially be unnecessary if you don't want to do anything with no results. You would still want to use it if you want to return a message like "no results found", of course..

编辑 - 添加

我觉得有一个问题也与行

I think there's a problem also with the line

if(dr1["CarId"] != DBNull.Value)

您应该使用DataReader的,如果公司的 IsDBNull以便()方法。在

You should be using if DataReader's IsDbNull() method. as in

if(dr.IsDbNull("CarId"))

对不起,我错过了第一次左右。

Sorry I missed that the first time around.

这篇关于DataReader的为空或空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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