简体   繁体   English

如何检查datareader是空还是空

[英]how to check if a datareader is null or empty

I have a datareader that return a lsit of records from a sql server database. 我有一个datareader从sql server数据库返回一个lsit记录。 I have a field in the database called "Additional". 我在数据库中有一个名为“Additional”的字段。 This field is 50% of the time empty or null. 此字段为空或空的50%。

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. 这背后的逻辑是:如果字段“附加”包含文本,则显示信息,否则隐藏字段。

I have tried: 我试过了:

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

The above code gives me this error: 上面的代码给了我这个错误:

Exception Details: System.IndexOutOfRangeException: Additional 异常详细信息: System.IndexOutOfRangeException:Additional

Any help would be greatly appreciated... 任何帮助将不胜感激...


See Also: 也可以看看:

Check for column name in a SqlDataReader object 检查SqlDataReader对象中的列名称

if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}
if (myReader.HasRows) //The key Word is **.HasRows**

{

    ltlAdditional.Text = "Contains data";

}

else

{   

    ltlAdditional.Text = "Is null Or Empty";

}

I haven't used DataReaders for 3+ years, so I wanted to confirm my memory and found this. 我没有使用DataReaders 3年多,所以我想确认我的记忆并找到了这个。 Anyway, for anyone who happens upon this post like I did and wants a method to test IsDBNull using the column name instead of ordinal number, and you are using VS 2008+ (& .NET 3.5 I think), you can write an extension method so that you can pass the column name in: 无论如何,对于像我这样发帖的人,想要一个方法来测试使用列名而不是序号的IsDBNull,并且你使用的是VS 2008+(我认为是.NET 3.5),你可以编写一个扩展方法这样您就可以传递列名:

public static class DataReaderExtensions
{
    public static bool IsDBNull( this IDataReader dataReader, string columnName )
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

Kevin 凯文

This is the correct and tested solution 这是正确且经过测试的解决方案

if (myReader.Read())
{

    ltlAdditional.Text = "Contains data";
}
else
{   
    ltlAdditional.Text = "Is null";
}

I also use OleDbDataReader.IsDBNull() 我也使用OleDbDataReader.IsDBNull()

if ( myReader.IsDBNull(colNum) ) { retrievedValue = ""; }
else { retrievedValue = myReader.GetString(colNum); }

First of all, you probably want to check for a DBNull not a regular Null . 首先,您可能想要检查DBNull而不是常规Null

Or you could look at the IsDBNull method 或者您可以查看IsDBNull方法

@Joe Philllips @Joe Philllips

SQlDataReader.IsDBNull(int index) requires the ordinal number of the column. SQlDataReader.IsDBNull(int index)需要列的序号。 Is there a way to check for nulls using Column Name, and not it's Ordinal Number? 有没有办法使用列名检查空值,而不是它的序号?

In addition to the suggestions given, you can do this directly from your query like this - 除了给出的建议,您可以直接从您的查询中执行此操作 -

SELECT ISNULL([Additional], -1) AS [Additional]

This way you can write the condition to check whether the field value is < 0 or >= 0. 这样您就可以编写条件来检查字段值是否为<0或> = 0。

This 这个

Example: 例:

objCar.StrDescription = (objSqlDataReader["fieldDescription"].GetType() != typeof(DBNull)) ? (String)objSqlDataReader["fieldDescription"] : "";

试试这个更简单的等价语法:

ltlAdditional.Text = (myReader["Additional"] == DBNull.Value) ? "is null" : "contains data";

AMG - Sorry all, was having a blond moment. AMG - 对不起,有一个金发碧眼的时刻。 The field "Additional" was added to the database after I had initially designed the database. 在我最初设计数据库之后,“Additional”字段被添加到数据库中。

I updated all my code to use this new field, however I forgot to update the actual datareader code that was making the call to select the database fields, therefore it wasn't calling "Additional" 我更新了所有代码以使用这个新字段,但是我忘了更新正在调用的实际datareader代码来选择数据库字段,因此它没有调用“Additional”

I also experiencing this kind of problem but mine, i'm using DbDataReader as my generic reader (for SQL, Oracle, OleDb, etc.). 我也遇到过这种问题,但我的,我正在使用DbDataReader作为我的通用阅读器(对于SQL,Oracle,OleDb等)。 If using DataTable, DataTable has this method: 如果使用DataTable,DataTable有这个方法:

DataTable dt = new DataTable();
dt.Rows[0].Table.Columns.Contains("SampleColumn");

using this I can determine if that column is existing in the result set that my query has. 使用这个我可以确定该列是否存在于我的查询所具有的结果集中。 I'm also looking if DbDataReader has this capability. 我也在寻找DbDataReader是否具备此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM