如何检查数据读取器是否为空或为空

发布于 2024-07-17 22:28:03 字数 634 浏览 6 评论 0原文

我有一个数据读取器,它从 sql server 数据库返回一组记录。 我的数据库中有一个名为“附加”的字段。 该字段 50% 的时间为空或为 null。

我正在尝试编写代码来检查该字段是否为空。 这背后的逻辑是: 如果“其他”字段包含文本,则显示信息,否则隐藏该字段。

我已经尝试过:

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

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

异常详细信息:System.IndexOutOfRangeException:附加

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


另请参阅:

检查 SqlDataReader 对象中的列名称< /p>

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.

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

Any help would be greatly appreciated...


See Also:

Check for column name in a SqlDataReader object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

多彩岁月 2024-07-24 22:28:04
if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}
if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}
空城仅有旧梦在 2024-07-24 22:28:04
if (myReader.HasRows) //The key Word is **.HasRows**

{

    ltlAdditional.Text = "Contains data";

}

else

{   

    ltlAdditional.Text = "Is null Or Empty";

}
if (myReader.HasRows) //The key Word is **.HasRows**

{

    ltlAdditional.Text = "Contains data";

}

else

{   

    ltlAdditional.Text = "Is null Or Empty";

}
箜明 2024-07-24 22:28:04

我已经 3 年多没有使用 DataReaders,所以我想确认我的记忆并找到了这个。 无论如何,对于像我一样看到这篇文章并想要一种使用列名而不是序号来测试 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

I haven't used DataReaders for 3+ years, so I wanted to confirm my memory and found this. 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:

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

Kevin

樱娆 2024-07-24 22:28:04

这是正确且经过测试的解决方案

if (myReader.Read())
{

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

This is the correct and tested solution

if (myReader.Read())
{

    ltlAdditional.Text = "Contains data";
}
else
{   
    ltlAdditional.Text = "Is null";
}
梦巷 2024-07-24 22:28:04

我还使用 OleDbDataReader.IsDBNull()

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

I also use OleDbDataReader.IsDBNull()

if ( myReader.IsDBNull(colNum) ) { retrievedValue = ""; }
else { retrievedValue = myReader.GetString(colNum); }
魂归处 2024-07-24 22:28:04

首先,您可能想要检查 DBNull 而不是常规的 Null

或者您可以查看 IsDBNull 方法

First of all, you probably want to check for a DBNull not a regular Null.

Or you could look at the IsDBNull method

稀香 2024-07-24 22:28:04

除了给出的建议之外,您还可以直接从查询中执行此操作,如下所示 -

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

这样您可以编写条件来检查字段值是否为 < 0 或 >= 0。

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.

多情癖 2024-07-24 22:28:04

@Joe Phillips

SQlDataReader.IsDBNull(int index) 需要列的序号。 有没有办法使用列名而不是序数来检查空值?

@Joe Philllips

SQlDataReader.IsDBNull(int index) requires the ordinal number of the column. Is there a way to check for nulls using Column Name, and not it's Ordinal Number?

玩世 2024-07-24 22:28:04

尝试这个更简单的等效语法:

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

Try this simpler equivalent syntax:

ltlAdditional.Text = (myReader["Additional"] == DBNull.Value) ? "is null" : "contains data";
心头的小情儿 2024-07-24 22:28:04

我也遇到过这种问题,但是我的,我使用 DbDataReader 作为我的通用阅读器(用于 SQL、Oracle、OleDb 等)。 如果使用 DataTable,DataTable 有这个方法:

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

使用这个方法我可以确定该列是否存在于我的查询的结果集中。 我也在寻找 DbDataReader 是否具有此功能。

I also experiencing this kind of problem but mine, i'm using DbDataReader as my generic reader (for SQL, Oracle, OleDb, etc.). If using DataTable, DataTable has this method:

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.

标点 2024-07-24 22:28:04

这个

例子:

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

This

Example:

objCar.StrDescription = (objSqlDataReader["fieldDescription"].GetType() != typeof(DBNull)) ? (String)objSqlDataReader["fieldDescription"] : "";
水染的天色ゝ 2024-07-24 22:28:04

最好在查询中完成此操作。 但是,如果查询绑定到其他函数,而不是在 while 循环中检查 DBNull.Value 将解决该问题。

if (reader.HasRows)
{
   while (reader.Read())
   {
      (reader["Additional"] != DBNull.Value) ? "contains data" : "is null";
   }
}

Best thing to get this done in query. However, if query bond to other functions than checking DBNull.Value in while loop would address the issue.

if (reader.HasRows)
{
   while (reader.Read())
   {
      (reader["Additional"] != DBNull.Value) ? "contains data" : "is null";
   }
}
智商已欠费 2024-07-24 22:28:04

AMG - 抱歉,我正在享受金发时刻。 在我最初设计数据库后,“附加”字段被添加到数据库中。

我更新了所有代码以使用这个新字段,但是我忘记更新调用选择数据库字段的实际数据读取器代码,因此它没有调用“附加”

AMG - Sorry all, was having a blond moment. The field "Additional" was added to the database after I had initially designed the database.

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文