我想测试SQL输出参数的NULL值。我意识到SQLString.NULL不等于DBNull.Value。我的问题是,是否有更通用的方法来检查参数的空值?
理想情况下,我不想根据其SQL类型检查每个参数,例如:整数参数SqlTypes.SqlInt32.Null。
以下是我的代码片段:
.ExecuteStoredProcedure("spGetClientDetails")
If CInt(.Parameters("@return_val").Value) = 0 Then
If myParams(1).SqlValue.Equals(SqlTypes.SqlString.Null) Or myParams(1).SqlValue.Equals(System.DBNull.Value) Then
Claimants = ""
Else...
答案 0 :(得分:2)
如果您使用Value
代替SqlValue
,则会获得常规的.NET类型而不是SqlType
。当数据库返回NULL
时,Value
将设置为DBNull.Value
。
检查DBNull.Value
的简明方法是TryCast
:
Dim Claimants As String = TryCast(myParams(1).Value, String)
如果Claimants
不是字符串,则会将Nothing
设置为myParams(1)
。要将Claimants
设置为空字符串,您可以添加If
:
Dim Claimants As String = If(TryCast(myParams(1).Value, String), "")