vb.net 检查数据库值是否为空 [英] vb.net check if database value is null
本文介绍了vb.net 检查数据库值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 vb.net 中运行查询,我想测试该列是否为空.我试过了:
i am running a query in vb.net and i want to test if the column is null. i have tried:
If reader.GetString(2) IsNot Nothing Then
If IsDBNull(reader.GetString(2)) Then
If reader.GetString(2) IsNot NULL Then
If NOT reader.GetString(2) IS Nothing Then
If NOT reader.GetString(2) IS NULL Then
但他们都回来了:
Data is Null. This method or property cannot be called on Null values.
当我在 MySQL 中运行查询时,列显示 NULL
when i run my query in MySQL, the columns show NULL
推荐答案
问题在于 GetString
方法在内部将您的行值转换为字符串.如果行值为空,则会出现异常.
The problem is the GetString
method that internally cast your row value to a string. If the row value is null you get the exception.
修复是使用 VB.NET 三元运算符
The fix is using the VB.NET ternary operator
Dim result = IF(reader.IsDbNull(2), "", reader.GetString(2))
或者如果您不想在第三个字段为空时分配默认值,您可以简单地编写
or if you don't want to assign a default value when the third field is null you could simply write
if Not reader.IsDbNull(2) Then
.......
End if
这篇关于vb.net 检查数据库值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文