Execute Scalar to trap error in case of no records returned

后端 未结 4 1562
轻奢々
轻奢々 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:04

    I try to avoid comparing a string to Nothing, even though it does work in VB.

    The Visual Basic .NET runtime evaluates Nothing as an empty string; that is, "". The .NET Framework, however, does not, and will throw an exception whenever an attempt is made to perform a string operation on Nothing.

    Plus pseudocoder's answer wont work as currently shown (oJobname is never set to anything)

    Dim lJobName as String = String.Empty
    Dim oJobName as object = SqlCommand.ExecuteScalar()
    
    If oJobName Is Nothing Then
        'Do something with the error condition
    Else
        lJobName = oJobName.ToString
    End If
    

提交回复
热议问题