简体   繁体   English

VB.Net没有捕获存储过程引发者

[英]VB.Net is not catching stored procedure raiserror

Has someone already encountered this error like the stored procedure raise an error but the client (vb.net) did not catch the error? 是否有人已经遇到此错误,例如存储过程引发了错误,但是客户端(vb.net)没有捕获到该错误?

Below is my code calling the stored procedure from vb.net 下面是我的代码从vb.net调用存储过程

Try

       Dim exec_refreshdependentviews As String = String.Format("EXEC REFRESHDEPENDENTVIEWS '{0}', '{1}', {2} ", tableName, usercode, isCreateNew)

       Using refreshCommand As New SqlClient.SqlCommand(exec_refreshdependentviews, DirectCast(sqlTransaction.Connection, SqlClient.SqlConnection), DirectCast(sqlTransaction, SqlClient.SqlTransaction))                  
               refreshCommand.ExecuteNonQuery()
       End Using
Catch exc As SqlClient.SqlException
        Throw New Exception("REFRESHDEPENDENTVIEWS", exc)
Catch ex As Exception
        Throw New Exception("REFRESHDEPENDENTVIEWS", ex)
End Try

Inside the stored procedure I raised an error, whose message was from the accumulated error encountered inside the SP. 在存储过程中,我引发了一个错误,该错误的消息来自SP内部遇到的累积错误。

RAISERROR  (@Errors, 16, 1)

Please note the @Errors has a value. 请注意@Errors有一个值。

When I tried to run the script thru backend, I can see error message message in Message tab. 当我尝试通过后端运行脚本时,可以在“消息”选项卡中看到错误消息。

EXEC RefreshDependentViews 'CustomerSalesOrder', 'admin', 1 

Below is the accumulated error message. 下面是累积的错误消息。

Msg 50000, Level 16, State 6, Procedure RefreshDependentViews, Line 216 Invalid object name 'dbo._Merged_SalesOrder_with_Details'. 消息50000,级别16,状态6,过程RefreshDependentViews,第216行无效的对象名称“ dbo._Merged_SalesOrder_with_Details”。 Msg 50000, Level 16, State 1, Procedure RefreshDependentViews, Line 216 Invalid object name 'Hips54.dbo.SupplierPurchaseReceiptDetailView'. 消息50000,级别16,状态1,过程RefreshDependentViews,第216行无效的对象名称“ Hips54.dbo.SupplierPurchaseReceiptDetailView”。 Msg 50000, Level 16, State 1, Procedure RefreshDependentViews, Line 299 Error running RefreshDependentViews CustomerSalesOrder: Failed to SP_REFRESHVIEW _Merged_SalesOrder_. 消息50000,级别16,状态1,过程RefreshDependentViews,第299行运行RefreshDependentViews CustomerSalesOrder时出错:未能执行SP_REFRESHVIEW _Merged_SalesOrder_。 Message: Invalid object name 'dbo._Merged_SalesOrder_with_Details'. 消息:无效的对象名称“ dbo._Merged_SalesOrder_with_Details”。 Error running RefreshDependentViews CustomerSalesOrder: Failed to SP_REFRESHVIEW _SOG_New. 运行RefreshDependentViews CustomerSalesOrder时出错:SP_REFRESHVIEW _SOG_New失败。 Message: Invalid object name 'Hips54.dbo.SupplierPurchaseReceiptDetailView'. 消息:无效的对象名称“ Hips54.dbo.SupplierPurchaseReceiptDetailView”。

The above error was not catch by the try-catch in vb.net. vb.net中的try-catch无法捕获上述错误。 It's like there's no error raised in my SP 就像我的SP中没有出现任何错误

I have simplified you code to just querying the exeption 我已将您的代码简化为仅查询exeption

Try
  Using refreshCommand As New SqlClient.SqlCommand("RAISERROR  ('test', 16, 1)", cn)
      refreshCommand.ExecuteNonQuery()
  End Using
Catch exc As SqlClient.SqlException
  Throw New Exception("REFRESHDEPENDENTVIEWS", exc)
Catch ex As Exception
  Throw New Exception("REFRESHDEPENDENTVIEWS", ex)
End Try

And it throws exception caught by the first catch as expected. 并按预期引发第一次捕获所捕获的异常。 So it seems that the problem is the stored procedure. 因此看来问题出在存储过程上。 I would try to change severity (16 -> 20) to supprest possible catches in server code. 我将尝试更改严重性(16-> 20),以阻止服务器代码中的可能捕获。

Thanks for the replies. 感谢您的答复。 I found now the culprit. 我现在找到了罪魁祸首。

In my code, I'm using the sqlTransaction in the SqlClient.SqlCommand, which is the cause of the issue why the raiserror wasn't catch by the try-catch block. 在我的代码中,我在SqlClient.SqlCommand中使用sqlTransaction,这就是为什么tryrr-catch块未捕获raiserror的问题的原因。

Below were my code that causes the issue: 以下是导致问题的我的代码:

sqlTransaction = sqlConnection.BeginTransaction(IsolationLevel.ReadCommitted)
Me.RefreshDependentViews(connectionString, objectsToRefresh(i), m_UserCode)
sqlTransaction.Commit()

My code in stored procedure 我在存储过程中的代码

DECLARE @Errors NVARCHAR(MAX), @Error NVARCHAR(100)
BEGIN TRY              
    BEGIN TRANSACTION
    EXEC RefreshTableColletionOnView @TableName, @UserCode
    COMMIT TRANSACTION          
END TRY              
BEGIN CATCH
    ROLLBACK TRANSACTION;

    SET @Error = 'Warning: Failed to RefreshDataDictionaryColumn ' + @tablename
    IF @Errors IS NULL
        SET @Errors = @Error + '.' + CHAR(13) + 'Message: ' + ERROR_MESSAGE() 
    ELSE
        SET @Errors = @Errors + @Error + '.' + CHAR(13) + 'Message: ' + ERROR_MESSAGE()

END CATCH

IF @Errors IS NOT NULL
BEGIN
    RAISERROR (@Errors, 16, 1)              
END

I don't know the reason why @Errors is empty when the SP was called thru VB.Net but if I run it thru MSSQL the @Errors has error value. 我不知道为什么通过VB.Net调用SP时@Errors为空的原因,但是如果我通过MSSQL运行@Errors则具有错误值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM