如何在 VB.Net 2.0 中检查 `SqlString` 是否为 Null?
How to check for Null on a `SqlString` in VB.Net 2.0?
我有一个 SqlString
类型的 属性 当前返回 Null。
_oData.Customer.Name
以下是我尝试过但没有奏效的检查。
IsNothing(_oData.Customer.Name)
_oData.Customer.Name.IsNull
_oData.Customer.Name.Value IsNot Nothing
_oData.Customer.Name = Nothing
每一个都会导致下面的错误。如何真正检查值为 Null。
Data is Null. This method or property cannot be called on Null values.
_oData.Customer
在其他属性上有值。
编辑
添加名称为 属性 的 class。下面是它在 Customer
class
中的定义
Private mName As SqlTypes.SqlString
Public Property Name() As SqlTypes.SqlString
Get
Return mName
End Get
Set(ByVal value As SqlTypes.SqlString)
mName = value
End Set
End Property
在 SqlTypes 命名空间中看到的 SqlString
EDIT-2
好的,这似乎是处理 ternery 操作的方式的问题
这个有效
If _oData.Customer.Name.IsNull Then
CName = ""
Else
CName = _oData.Customer.Name
End If
这不是
CName = IIf(_oData.Customer.Name.IsNull, "", _oData.Customer.Name.Value)
谁能告诉我为什么这在使用三元运算时不起作用?
你应该使用 Microsoft.VisualBasic.Information.IsDBNull() 方法:
If IsDBNull(_oData.Customer.Name) Then
我有一个 SqlString
类型的 属性 当前返回 Null。
_oData.Customer.Name
以下是我尝试过但没有奏效的检查。
IsNothing(_oData.Customer.Name)
_oData.Customer.Name.IsNull
_oData.Customer.Name.Value IsNot Nothing
_oData.Customer.Name = Nothing
每一个都会导致下面的错误。如何真正检查值为 Null。
Data is Null. This method or property cannot be called on Null values.
_oData.Customer
在其他属性上有值。
编辑
添加名称为 属性 的 class。下面是它在 Customer
class
Private mName As SqlTypes.SqlString
Public Property Name() As SqlTypes.SqlString
Get
Return mName
End Get
Set(ByVal value As SqlTypes.SqlString)
mName = value
End Set
End Property
在 SqlTypes 命名空间中看到的 SqlString
EDIT-2
好的,这似乎是处理 ternery 操作的方式的问题
这个有效
If _oData.Customer.Name.IsNull Then
CName = ""
Else
CName = _oData.Customer.Name
End If
这不是
CName = IIf(_oData.Customer.Name.IsNull, "", _oData.Customer.Name.Value)
谁能告诉我为什么这在使用三元运算时不起作用?
你应该使用 Microsoft.VisualBasic.Information.IsDBNull() 方法:
If IsDBNull(_oData.Customer.Name) Then