vb.net 在没有返回记录的情况下执行标量以捕获错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13253109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:02:49  来源:igfitidea点击:

Execute Scalar to trap error in case of no records returned

vb.netado.net

提问by Conrad Jagger

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?

如果没有找到记录,问题如何捕获?

采纳答案by PatFromCanada

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

我尽量避免将字符串与 Nothing 进行比较,即使它在 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.

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

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

加上伪编码器的答案不会像当前显示的那样工作(oJobname 从未设置为任何内容)

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

回答by finch

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

无论手册怎么说,与Nothing相比都行不通。所以If lJobName Is Nothing当结果集为空时不会被触发。

Comparing to DBNull.Value DID work for me:

与 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

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

值得注意的是,当结果集为空(即没有找到记录)时,这不是“错误”。

回答by pseudocoder

ExecuteScalar() returns Nothingif there is an empty result set, which should be preserved when assigning into a string, so I would try this:

Nothing如果存在空结果集,则ExecuteScalar() 返回,在分配给字符串时应保留该结果集,因此我会尝试以下操作:

Dim lJobName as String = String.Empty
lJobName = SqlCommand.ExecuteScalar()
If lJobName Is Nothing Then
    'Do something with the error condition
Else
    'Do something with lJobName which contains a valid result.
End If

Of course this doesn't help you if you cause a SqlException, but it should handle the problem of no rows found in the result set.

当然,如果您导致 SqlException,这对您没有帮助,但它应该处理在结果集中找不到行的问题。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

回答by Oskar

I think it is possible to handle this situation in the statement of the query:

我认为可以在查询语句中处理这种情况:

SELECT ISNULL(FieldID,"0") FROM TableName

SELECT ISNULL(FieldID,"0") FROM TableName

and subsequently in the program validate the result:

然后在程序中验证结果:

if then ...... else ...... endif

如果那么......否则...... endif