C# 如何检查数据读取器是否为空或为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/762861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a datareader is null or empty
提问by Jason
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.
我有一个 datareader,它从 sql server 数据库返回记录的 lsit。我在数据库中有一个名为“附加”的字段。该字段有 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:附加
Any help would be greatly appreciated...
任何帮助将不胜感激...
See Also:
也可以看看:
采纳答案by Robert Durgin
if (myReader["Additional"] != DBNull.Value)
{
ltlAdditional.Text = "contains data";
}
else
{
ltlAdditional.Text = "is null";
}
回答by Joe Phillips
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
方法
回答by Kirtan
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。
回答by Jason
AMG - Sorry all, was having a blond moment. The field "Additional" was added to the database after I had initially designed the database.
AMG - 对不起,有一个金发碧眼的时刻。在我最初设计数据库后,“附加”字段被添加到数据库中。
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"
我更新了所有代码以使用这个新字段,但是我忘记更新调用选择数据库字段的实际数据读取器代码,因此它没有调用“附加”
回答by Shiva
@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?
SQlDataReader.IsDBNull(int index) 需要列的序号。有没有办法使用列名检查空值,而不是序数?
回答by Kevin Nelson
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:
我已经 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
凯文
回答by Ahmed Fahmy
This is the correct and tested solution
这是正确且经过测试的解决方案
if (myReader.Read())
{
ltlAdditional.Text = "Contains data";
}
else
{
ltlAdditional.Text = "Is null";
}
回答by Rob
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:
我也遇到过这种问题,但我的,我使用 DbDataReader 作为我的通用阅读器(用于 SQL、Oracle、OleDb 等)。如果使用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 是否具有此功能。
回答by catalyst
I also use OleDbDataReader.IsDBNull()
我也使用 OleDbDataReader.IsDBNull()
if ( myReader.IsDBNull(colNum) ) { retrievedValue = ""; }
else { retrievedValue = myReader.GetString(colNum); }
回答by Kemal Can ?Z?EL?K
if (myReader.HasRows) //The key Word is **.HasRows**
{
ltlAdditional.Text = "Contains data";
}
else
{
ltlAdditional.Text = "Is null Or Empty";
}