如何找出 ExecuteScalar() 是 dbnull

how to find out ExecuteScalar() is dbnull

如何查看ExecuteScalar()的结果是否没有数据?检查是否为空无效。

我的代码:

Dim cmdCode As New OracleCommand
cmdCode.Connection = sgcnn
cmdCode.CommandText = "select max(SUBSTR(client_code,4,7)) from CLIENT_INFO where (client_code Like '%' || :SysUser || '%') "
cmdCode.Parameters.Add("@SysUser", OracleDbType.Varchar2).Value = strMessage

'Dim sqlResult As Object = cmdCode.ExecuteScalar()
'If sqlResult Is Nothing Then
'    ' No result found
'    strMessage.Append("0001")
'    txtClientCode.Text = strMessage.ToString
'Else
'    'result found
'    Dim newProdID As Int32 = 0
'    newProdID = Convert.ToInt32(cmdCode.ExecuteScalar())
'    newProdID = newProdID + 1
'    strMessage.Append(newProdID.ToString("0000"))
'    txtClientCode.Text = strMessage.ToString
'End If
If sqlResult Is DBNull.Value Then

一旦确定它不是 NULL,就不要再调用 ExecuteScalar。你已经得到了结果,所以使用它。此外,它已经是 Integer,因此无需转换。刚投:

Dim newProdID = CInt(sqlResult) + 1
        If IsDBNull(cmdCode.ExecuteScalar()) Then
        '    ' No result found
        '    strMessage.Append("0001")
        '    txtClientCode.Text = ""
    Else
        '    'result found
        '    Dim newProdID As Int32 = 0
        '    newProdID = Convert.ToInt32(cmdCode.ExecuteScalar())
        '    newProdID = newProdID + 1
        '    strMessage.Append(newProdID.ToString("0000"))
        '    txtClientCode.Text = strMessage.ToString
    End If