vb.net 无和 system.DBNull 的区别

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

Difference between nothing and system.DBNull

vb.netwinformswinapp

提问by Jay Tankariya

I am working in VB.NET and I am wondering about the difference between Nothingand System.DBNull.

我在 VB.NET 中工作,我想知道Nothing和之间的区别System.DBNull

When I fire save query at that time I am giving value from grid at runtime like as follow:

当我当时触发保存查询时,我在运行时从网格给出值,如下所示:

gvMain.Rows(j).Cells("Brand").Value.ToString()

But it shows me error when it has value of Nothingand it works perfectlly when it has value of System.DBnull.

但是当它的值为 时它会向我显示错误,当它的值为Nothing时它会完美地工作System.DBnull

What to do in this case?
Thanks in advance

在这种情况下该怎么办?
提前致谢

回答by SysDragon

The keyword Nothingis used to specify or asign that a var of reference type is not pointing anything, no object is instanciated for this var.

该关键字Nothing用于指定或分配引用类型的 var 不指向任何内容,没有为此 var 实例化任何对象。

DBNull.Value, on the other hand, is an object used to point out that a type of a field of the DataBase is of null value.

DBNull.Value,另一方面,是一个对象,用于指出DataBase的某个字段的类型为空值。

回答by Donald.Record

Nothingis of Type System.Object.

Nothing是类型 System.Object.

It represents the default value of any data type.

它代表任何数据类型的默认值。

DBNull.Valueis of Type System.DBNull

DBNull.Value是类型 System.DBNull

If something shows up as System.DBNull, that means that even though it doesn't have a value, it has a valid pointer. As you may have found out, it cannot be converted to a string, integer, etc. You must do a check (preferably using IsDBNull.

如果某些内容显示为System.DBNull,则意味着即使它没有值,它也有一个有效的指针。您可能已经发现,它不能转换为字符串、整数等。您必须进行检查(最好使用IsDBNull.

If IsDBNull(gvMain.Rows(j).Cells("Brand").Value) Then
    Return String.Empty
Else
    Return gvMain.Rows(j).Cells("Brand").Value.ToString().Trim()
End If