调用存储过程

时间:2019-04-09 20:44:36

标签: c# sql database stored-procedures management-studio-express

我在AdventureWorks数据库中具有带有事务的存储过程,我需要从C#控制台应用程序调用此过程,以验证存储过程是否适当地提交或回滚了事务。

USE [AdventureWorks]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.uspUpdateTwoProducts
    (
    @prmProductID1 int,
    @prmProductID2 int
    )
AS

    DECLARE @varSuccess BIT
    SELECT @varSuccess = 0

    BEGIN TRANSACTION

    UPDATE Production.Product
        SET ListPrice = ListPrice * 1.1
    WHERE
        ProductID=@prmProductID1

    IF (@@ERROR = 0 AND @@ROWCOUNT = 1)
    BEGIN

        UPDATE Production.Product
            SET ListPrice = ListPrice * 0.9
        WHERE
            ProductID=@prmProductID2

        IF @@ERROR = 0 AND @@ROWCOUNT = 1
            SELECT @varSuccess = 1

    END

    IF @varSuccess = 1
        COMMIT TRANSACTION
    ELSE
        ROLLBACK TRANSACTION

    RETURN

0 个答案:

没有答案