Execute Scalar to trap error in case of no records returned

后端 未结 4 1505
轻奢々
轻奢々 2021-01-20 06:33

I have got this code below:

Dim lJobName As String = \"\"
SQLCommand.CommandText = \"Select JobName from Jobs where Id = @Id \"
SQLCommand.Parameters.Add(New         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-20 07:26

    Whatever the manual says, comparing to Nothing does not work. So If lJobName Is Nothing will NOT be triggered when the result set is empty.

    Comparing to DBNull.Value DID work for me:

    If lJobName Is DBNull.Value Then
        'Do something with the error condition
    Else
        'Do something with lJobName which contains a valid result.
    End If
    

    It is worth noting that when the result set is empty (i.e. no records were found), this is not an "error".

提交回复
热议问题