我有下面这段代码:
Dim lJobName As String = ""
SQLCommand.CommandText = "Select JobName from Jobs where Id = @Id "
SQLCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.Int))
SQLCommand.Parameters(0).Value = var_id
lJobName = SQLCommand.ExecuteScalar()
问题是没有记录怎么抓?
最佳答案
无论手册怎么说,与 Nothing 进行比较是行不通的。因此,当结果集为空时,If lJobName Is Nothing
将不会被触发。
与 DBNull.Value 相比,DID 对我有用:
If lJobName Is DBNull.Value Then
'Do something with the error condition
Else
'Do something with lJobName which contains a valid result.
End If
值得注意的是,当结果集为空时(即没有找到记录),这不是“错误”。
关于vb.net - 在没有记录返回的情况下执行标量以捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253109/