views:

156

answers:

1

Hi. If i run stored procedure in ADO.NET with transaction enabled and SP begin its own transaction inside(with COMMIT TRANS). What happens when ADO.NET rollback transaction? Is transaction from SP rollbacked as well? Is DB in state as it was before calling ADO.NET?

Thank you.

+2  A: 

I have just tested this using C# 2008, Sql Server 2005 using Code as below and it did rollback.

SqlConnection con = new SqlConnection("server=svr;database=db;uid=user;pwd=pw;Connect Timeout=900");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MySp";
con.Open();
SqlTransaction trans = con.BeginTransaction();
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Rollback();  // or trans.Commit()
con.Close();

Database code

CREATE TABLE [dbo].[TEST](
    [Val] [int] NULL
)

ALTER PROCEDURE MySp
AS 
BEGIN
    BEGIN TRANSACTION T1
    INSERT INTO TEST SELECT 1
    COMMIT TRANSACTION T1
END
astander
+1. It had to be the case, right. But +1 for actually doing it!
Mitch Wheat
I wanted to venture a guess, and then thought screw it, lets just see for real X-).
astander