- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 SQL 数据读取器(在 vb.net 中)通过存储过程从 SQL Server 2008 数据库中提取文章对象。该对象的一部分包括如下所示的两个属性:
theArticle.Truthfulness = ((myReader.GetInt32(myReader.GetOrdinal("Truthfulness"))))
theArticle.Relevance = ((myReader.GetInt32(myReader.GetOrdinal("Relevance"))))
我的问题是真实性和相关性可能会返回空值,这会导致函数失败。
我想我明白为什么。我要求提供一个整数值 (getin32),因为返回 null,所以失败。
如何容纳数据库中的空值以免其掉落?
最佳答案
您可以使用 .IsDBNull()
检查给定序数位置是否为空,然后执行某些操作 - 例如将您的值设置为 -1 或其他值:
int myOrdinal = myReader.GetOrdinal("Truthfullness");
if(myReader.IsDBNull(myOrdinal))
{
theArticle.Truthfulness = -1;
}
else
{
theArticle.Truthfulness = myReader.GetInt32(myOrdinal);
}
正如 Mike Hofer 在他的回答中指出的那样,您还可以将所有这些逻辑包装到扩展方法中:
public static class SqlDataReaderExtensions
{
public static int SafeGetInt32(this SqlDataReader reader,
string columnName, int defaultValue)
{
int ordinal = reader.GetOrdinal(columnName);
if(!reader.IsDbNull(ordinal))
{
return reader.GetInt32(ordinal);
}
else
{
return defaultValue;
}
}
}
然后只需使用“SafeGetInt32”方法即可:
theArticle.Truthfulness = myReader.SafeGetInt32("Truthfullness", -1);
马克
关于sql - 如果值为空,如何将值返回到 sqldatareader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1513438/
如何在另一个 SqlDataReader 中实现一个 SqlDataReader? 我的问题是我有一个 SqlDataReader。我正在发出 while (reader.read()) 并且在 wh
作为 C# 新手,我在不同线程之间传递 SqlDataReader 时遇到了这个“难题”。无需过多讨论,其想法是让主线程从数据库(大型记录集)中获取数据,然后让辅助任务逐条记录地运行该记录,并根据其内
在命名空间System.Data.SqlClient下,我们都有 SqlDataReader.GetSqlBinary 和 SqlDataReader.GetSqlBytes . 两者似乎都提供了“原
更新:事实证明反射也不一定很慢。使用 Fasterflect (http://www.codeproject.com/Articles/38840/Fasterflect-a-fast-and-sim
一个被认为是更好的标准?一个比另一个快吗?或者,主要是偏好? GetOrdinal 很好,因为您可以自己调用列名,而不必担心在 SQL 中计算字段的索引,但我想知道使用其中一个是否比另一个有好处。 按
这两种方法的主要区别是什么?在 msdn 网站上,它的解释如下,但我不明白。 Read 将 SqlDataReader 前进到下一条记录。 (覆盖DbDataReader.Read().) NextR
我想从数据库中检索十进制值,我想知道检查空值的推荐方法是什么。 我在 MSDN - DBNull.Value Field 上看到过很少使用此检查。 因此,reader.IsDBNull 是检查空值的最
我正在为我的数据访问层编写单元测试。为此,我为 SqlCommand (ISqlCommand) 创建了一个包装器,以便我可以模拟其功能。 ISqlCommand command = _connect
我有一个存储过程,其中包含用于返回记录的 ORDER BY 子句。当我在 SQL 中执行时,我看到适当的顺序。 但是,当我从客户端执行并加载到 SqlDataReader 时,顺序发生了变化。这不是预
从值为 null 的读取器转换日期时间时出现问题。 form._date101 = reader[52] == DBNull.Value ? DBNull.Value : (DateTime?)rea
有谁知道 DbDataReaders 是如何工作的。我们可以以SqlDataReader为例。 当您执行以下操作时 cmd.CommandText = "SELECT * FROM Customers
使用以下语法从SqlDataReader读取值之间有什么区别: Dim reader As SqlClient.SqlDataReader reader("value").ToString() 要么
我在 MSSMS 中执行了约 2 秒的查询(返回 25K 行) .NET (sqlReader) 中使用的相同查询执行几分钟! 我也尝试过只执行阅读器 (注释了 while 循环中的所有代码,只留下
我有一个问题,我列出了 100 多个学生,但读取器只检索到表中的最后一个学生。 string selectQuery = "SELECT * FROM Students WHERE firstName
我的项目是一个 Windows 服务,我在从我的数据库返回值时遇到了问题,所以我将这段代码分离到一个控制台应用程序中以便于调试,但在我的服务中不起作用的代码在控制台应用程序。 所以在我的服务中我有这个
我在数据库记录 1 和记录 2 中有两条记录。datareader 将返回记录 1 的 guid 值,代码工作正常。但在记录 2 上,数据读取器将返回“”。我的问题是如何无一异常(exception)
我有一个使用 SQLLite 的数据库,我正在使用 C#。我进行了查询并且它有效,但是当查询有 ( ) 时它失败并给出了关于 SQL 语法的异常。 我创建了一个函数来帮助我使用 SQLiteDataR
我想用对象填充列表。这些对象包含以下格式的信息: class record { //klassen variable private string voornaam; priv
能否将 SqlDataReader 传递给 session 或发送给客户端? 例如,如果我从数据库中检索了一些行,并想将这些数据发送到另一台客户端机器。我可以通过在服务器上使用 json 序列化它然后
我正在尝试通过引用传递给读者或让读者返回。我在返回时遇到了问题。 public static SqlDataReader GetSql(string businessUnit, string task
我是一名优秀的程序员,十分优秀!