如果没有返回记录,则执行标量捕获错误 [英] Execute Scalar to trap error in case of no records returned

查看:77
本文介绍了如果没有返回记录,则执行标量捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面得到了这段代码:

I have got this code below:

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()

如果找不到记录,如何捕获?

Issue how to catch if no records are found?

推荐答案

即使在VB中它确实可以工作,我也尽量避免将字符串与Nothing进行比较。

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

Visual Basic .NET运行时将Nothing评估为空字符串;那是, 。但是,.NET Framework不会,并且在尝试对Nothing进行字符串操作时将抛出异常。

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.

加上伪编码器的答案无法正常工作显示(oJobname从未设置为任何值)

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

这篇关于如果没有返回记录,则执行标量捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆