如何检查数据读取器是否为null或为空 [英] how to check if a datareader is null or empty

查看:50
本文介绍了如何检查数据读取器是否为null或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据读取器,它从sql服务器数据库返回一个lsit记录.我在数据库中有一个名为"Additional"的字段.此字段是50%的时间为空或为空.

I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time empty or null.

我正在尝试编写代码以检查此字段是否为空.其背后的逻辑是:如果字段其他"包含文本,然后显示信息,否则隐藏字段.

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

我尝试过:

if (myReader["Additional"] != null)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}

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

The above code gives me this error:

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

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

Any help would be greatly appreciated...

检查SqlDataReader对象中的列名

推荐答案

if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}

这篇关于如何检查数据读取器是否为null或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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