0

这是我正在使用的示例存储过程

CREATE PROCEDURE [dbo].[CreateCustomer]
        @firstName nvarchar(50) = null,
        @lastName nvarchar(50) = null,
        @emailAddress nvarchar(50),
        @contactNumber varchar(15) =null,
        @street1 nvarchar(50) = null,
        @street2 nvarchar(50) = null,
        @city nvarchar(50) = null,
        @State nvarchar(50)= null,
        @Country nvarchar(50)=null,
        @postalCode varchar(5) = null,
        @middleName nvarchar(50),
        @password nvarchar(50),
        @membershipExpieryDate datetime,
        @status int OUT

    AS
        BEGIN TRY
            BEGIN TRANSACTION
            SAVE TRANSACTION initialP
                DECLARE @lastAccessed datetime = GETDATE()
                DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()
                BEGIN TRANSACTION
                    INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
                        Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
                        VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
                         @city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)



     INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V') 
            COMMIT

            SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
                Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress  

        SET @status =1
        COMMIT
    END TRY 
    BEGIN CATCH
        IF(@@TRANCOUNT > 0)
        BEGIN
            ROLLBACK TRANSACTION initialP
        END
        SET @status =2
    END CATCH

这是我用来获取查询数据的代码:

ObjectParameter status = new ObjectParameter("status", typeof(int));
var dataSet = Entity.CreateCustomer(newCustomer.FirstName, newCustomer.LastName,
newCustomer.EmailAddress, newCustomer.ContactNumber, newCustomer.Street1, newCustomer.Street2,
newCustomer.City, newCustomer.State, newCustomer.Country, newCustomer.PostalCode, newCustomer.MiddleName,
newCustomer.Password, newCustomer.MembershipExpieryDate, status);

但是当执行这个存储过程时,它会给出一个错误,说 Trans count 不匹配。据我所知,当 a 发生时,它会被 1归罪。trans count当 a发生时,它会被 1减刑。我已经在函数导入中映射了该函数,因为它可以返回一个复杂类型。begin transactioncommit

我无法理解这个问题。对我来说,这似乎还可以。我想知道天气我这样做是否正确与一些细节。谢谢。

编辑:这是我通过编写示例代码发现的。我首先认为是开始交易和提交。但事实并非如此。它允许选择甚至在提交语句之前插入的值(如果我错了,请纠正我。)。我创建了一个包含 3 列(test1 唯一标识符、test2 int、test3 int)的测试表,并创建了一个存储过程调用测试:它在下面给出:

CREATE PROCEDURE dbo.TestForCommit

    (
    @test11 int,
    @test111 int,
    @status int OUTPUT
    )

AS
    begin try
        declare @test1 uniqueidentifier  = NEWID()
        begin transaction
            insert into TestT1(Test1, Test11, Test111) values(@test1, @test11, @test111)
        commit
        select Test1, Test11, Test111 from TestT1 where Test1 = @test1
        SET @status = 1
    end try
    begin catch
        Rollback Transaction
        set @status =2
    end catch
    /* SET NOCOUNT ON */
    RETURN

我在实体模型中创建了一个功能导入,我像这样使用它:

            ObjectParameter status = new ObjectParameter("status", typeof(int));
            SchoolEntities test = new SchoolEntities();
            var dataSet = test.TestForCommit(1, 1, status);
                //A:
                //(XXXXXXXXXXXXX)Console.WriteLine((int)status.Value);
                foreach (var item in dataSet)
                {
                    TestForCommit_Result t = item;
                    Console.WriteLine("{0} , {1} , {2}", t.Test1, t.Test11, t.Test111);

                }
                //B:
                Console.WriteLine((int)status.Value);
                Console.ReadLine();

这个 //A: 部分导致了问题。这给出了一个空引用异常。但是 //B: 部分正在工作并给出状态值。我仍在寻找为什么它会这样工作。谢谢!!

4

1 回答 1

1

我的建议。(设置 Xact_Abort 开)

Begin Try
    Set NoCount On
    Set Xact_Abort On
    Begin Tran
        --Your Query
    Commit Tran
End Try

Begin Catch
    Rollback Tran
End Catch

CREATE PROCEDURE [dbo].[CreateCustomer]
    @firstName nvarchar(50) = null,
    @lastName nvarchar(50) = null,
    @emailAddress nvarchar(50),
    @contactNumber varchar(15) =null,
    @street1 nvarchar(50) = null,
    @street2 nvarchar(50) = null,
    @city nvarchar(50) = null,
    @State nvarchar(50)= null,
    @Country nvarchar(50)=null,
    @postalCode varchar(5) = null,
    @middleName nvarchar(50),
    @password nvarchar(50),
    @membershipExpieryDate datetime,
    @status int OUT

AS
BEGIN TRY
    Set NoCount ON
    Set XAct_Abort ON
    DECLARE @lastAccessed datetime = GETDATE()
    DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()

    BEGIN TRANSACTION
    INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
    Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
    VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
    @city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)

    INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V') 
    COMMIT

    SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
    Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress  

    SET @status =1
    COMMIT Tran
END TRY 
BEGIN CATCH
    ROLLBACK TRANSACTION
    SET @status =2
END CATCH
于 2012-07-30T10:55:39.000 回答