SQL vb.net - 从数据库读取空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16167056/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:57:30  来源:igfitidea点击:

vb.net - read null value from database

sqlvb.net

提问by Milkncookiez

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:

我有一个数据库,其中列中没有值(所以是null),但我无法在 vb.net 中处理此问题。我尝试使用此代码:

            reader.Read()
            If String.IsNullOrEmpty(reader.GetString(0)) Then
                Return
            Else
                tilbulfolderTextBox.Text = reader.GetString(0)
            End If

and also with:

还有:

If reader.Read() = False Then

and with:

与:

If IsDBNull(reader.Read()) Then

But apparently it doesn't work because I get exception on the statement after the Elsethat I can't get Nullvalues with this method. I guess you will figure out what I require from the program by reading the code itself.

但显然它不起作用,因为我在Else无法Null使用此方法获取值之后的语句中出现异常。我想你会通过阅读代码本身来弄清楚我对程序的要求。

回答by Steve

The IsDBNullmethod 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)

DbDataReader 基对象的IsDBNull方法被定义为处理这种情况。
当然,如果 reader.Read() 返回 false(意味着没有更多行可用),则您不能尝试读取某些内容

    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

另外,我没有看到您的更多代码,但请记住,如果您不关闭连接并处理此操作中涉及的对象,以这种方式返回可能是非常错误的

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

而且,是的,正如其他人指出的那样,Microsoft.VisualBasic 程序集中还有一个名为 IsDBNull 的函数,但是,我更喜欢使用 .NET 框架中定义的类提供的方法,而不是为与以前版本的 VB

回答by Egor4eg

You should use IsDbNullfunction to comapre it with null:

您应该使用IsDbNull函数将其与null

        reader.Read()
        If IsDbNull(reader(0)) OrElse String.IsNullOrEmpty(reader.GetString(0)) Then
            Return
        Else
            tilbulfolderTextBox.Text = reader.GetString(0)
        End If

回答by Mandeep Singh

try this

尝试这个

reader.Read()
        If IsDbNull(reader.GetString(0)) Then
            Return
        Else
            tilbulfolderTextBox.Text = reader.GetString(0)
        End If

回答by SysDragon

.Net has a different type for handling SQL NULLs: DbNull.

.Net 有一种不同的类型来处理 SQL NULL:DbNull.

Check this post about how to handle it: handling dbnull data in vb.net

查看这篇关于如何处理它的帖子:处理 vb.net 中的 dbnull 数据

In your case, you need the IsDbNullfunction, check this MSDN Documentationabout it.

在您的情况下,您需要该IsDbNull功能,请查看有关它的MSDN 文档

If IsDbNull(data) Then
    return
Else
    tilbulfolderTextBox.Text = data
End If

回答by Rajaprabhu Aravindasamy

use IsDBNullfunction to ensure that the value from DataReader is null or not. Then proceed the code flow.

使用IsDBNull函数确保来自 DataReader 的值是否为空。然后继续代码流程。

回答by Cubsoft

Sounds like your IF may not be working properly

听起来您的 IF 可能无法正常工作

Try using IsDBNull instead.

尝试改用 IsDBNull。

IsDBNull is a boolean variable and can be used like the following;

IsDBNull 是一个布尔变量,可以像下面这样使用;

reader.Read()

If Not IsDbNull(reader.GetString(0)) Then
    tilbulfolderTextBox.Text = reader.GetString(0)
Else
    return
End If