如何尝试 - 在事务提交或回滚时捕获或返回成功标志 - SQL Server(How to Try-Catch or return a success flag when Transaction commits or rollbacked - SQL Server)

我在存储过程中有一个SQL事务,我想知道如何在事务成功提交或在任何时候失败的情况下返回成功消息,即。 rollbacked,我想在我的代码在MVC C#中使用该标志。

I have a SQL Transaction in a Stored procedure , I want to know how to return a success message in case of Transaction successfully commits or failed at any point ie. rollbacked,I want to use that flag in my code in MVC C# .

最满意答案

下面的代码可能会帮助你,

BEGIN TRY BEGIN TRANSACTION -- sql update\delete statement goes here COMMIT TRANSACTION END TRY BEGIN CATCH IF (@@TRANCOUNT > 0) BEGIN ROLLBACK TRANSACTION PRINT 'Error ...' END SELECT ERROR_NUMBER() AS Error#, ERROR_MESSAGE() AS ErrorMsg END CATCH

Below code might help you on this,

BEGIN TRY BEGIN TRANSACTION -- sql update\delete statement goes here COMMIT TRANSACTION END TRY BEGIN CATCH IF (@@TRANCOUNT > 0) BEGIN ROLLBACK TRANSACTION PRINT 'Error ...' END SELECT ERROR_NUMBER() AS Error#, ERROR_MESSAGE() AS ErrorMsg END CATCH

更多推荐