vb.net - 从数据库读取空值 [英] vb.net - read null value from database
问题描述
我有一个数据库,其中列中没有值(所以它是 null
),但我无法在 vb.net 中处理这个问题.我尝试使用此代码:
I have a database where in column there is no value (so it's null
), but I can't handle this in vb.net. I tried with this code:
reader.Read()
If String.IsNullOrEmpty(reader.GetString(0)) Then
Return
Else
tilbulfolderTextBox.Text = reader.GetString(0)
End If
还有:
If reader.Read() = False Then
并与:
If IsDBNull(reader.Read()) Then
但显然它不起作用,因为我在 Else
之后的语句中出现异常,我无法使用此方法获取 Null
值.我想你会通过阅读代码本身来了解我对程序的要求.
But apparently it doesn't work because I get exception on the statement after the Else
that I can't get Null
values with this method.
I guess you will figure out what I require from the program by reading the code itself.
推荐答案
IsDBNull 方法被定义为处理这种情况.
当然,如果 reader.Read() 返回 false(意味着没有更多行可用),则您不能尝试读取某些内容
The IsDBNull method of the DbDataReader base object is defined to handle this situation.
Of course you can't try to read something if the reader.Read() returns with false (meaning no more rows are available)
If reader.Read() Then
If reader.IsDBNull(0) Then
Return
Else
tilbulfolderTextBox.Text = reader.GetString(0)
End If
End If
另外,我没有看到你的更多代码,但请记住,如果你不关闭连接并处理此操作中涉及的对象,以这种方式返回可能是非常错误的
Also, I don't see more of your code, but keep in mind that returning in this way could be very wrong if you don't close the connection and dispose the objects involved in this operation
而且,是的,正如其他人所指出的,Microsoft.VisualBasic 程序集中还有一个名为 IsDBNull 的函数,但是,我更喜欢使用 .NET 框架中定义的类提供的方法,而不是提供的方法与以前版本的 VB 兼容
And, yes, as others have pointed out, there is also a function called IsDBNull from the Microsoft.VisualBasic assembly, but, I prefer to use the methods provided by the classes defined in the .NET framework and not the ones provided for compatibility with previous versions of VB
这篇关于vb.net - 从数据库读取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!