Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 Transaction problem

Author  Topic 

zurbum
Yak Posting Veteran

55 Posts

Posted - 2004-03-06 : 12:11:57
I use ado and I develop in c++ language. CAdoDatabase is the class I made so I don't need to write a lot of code again and again. Iam connecting to the SQL 2001.
During development of an application I came to this problem while using transcations.

CAdoDatabase ado;
ado.Open();
ado.ExecuteCommand("BEGIN TRANSACTION tran1");
ado.ExecuteCommand("INSERT INTO EMPLOYEE (E_NAME) VALUES ('John')");
ado.ExecuteCommand("SELECT @@trancount as tc");
-->here @@trancount is 1 as it should be

ado.ExecuteCommand("SELECT * FROM EMPLOYEE");
ado.ExecuteCommand("SELECT @@trancount as tc");
-->here @@trancount is 0, but why???
calling here ROLLBACK or COMMIT TRANSCATION tran1 raises an error

but if I add this
ado.ExecuteCommand("INSERT INTO SOMETHING (SMT) VALUES ('test')");
ado.ExecuteCommand("SELECT @@trancount as tc");
-->here @@trancount is again 1 as it should be

ado.ExecuteCommand("ROLLBACK TRANSACTION tran1");
ado.Close();


Can anyone explain me why this happens.

vganesh76
Yak Posting Veteran

64 Posts

Posted - 2004-05-26 : 00:10:29
Try the same thing as a single statement by concating the individual statement into a single string like
SQLStr = "BEGIN TRANSACTION tran1"
SQLStr = SQLStr & " INSERT INTO EMPLOYEE (E_NAME) VALUES ('John')"
SQLStr = SQLStr & "SELECT @@trancount as tc"
...
then do ado.Execute (SQLStr)

it should work

Enjoy working
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-05-26 : 05:02:44
Why wouldn't this happen? You are forcing a transaction through by doing an insert, but if you want a transaction to be opened for the select you need to wrap it in begin...end...
Go to Top of Page
   

- Advertisement -