SQL transaction rollback problem
-
hi,i am new to visual C++ programming. i have a simple visual C++ program that involves basic sql transactions and if a query fails then i have tried to rollback all the previous transactions as well. I am using MS SQL Server. the problem is that the rollback is not working in the tables in the server while when i run the program in the debug mode it shows that the rollback feature is working as it returns a positive integer from the rollback() function. the program shows no errors or warnings. Can anyone provide some suggestions? For testing i have used three queries in which the third one has been intentionally left wrong. I need the first two queries to be rollbacked when the error occurs in the third query. Would be glad if you can point out other errors as i am still learning C++ programming... Attached is the code:
class test { public: void testmethod() { CDatabase dbobject[100]; static int counter=0; char query [3][100]={{"Insert into product values (1,'keyboard')"},{"Insert into product values (2,'mouse')"},{"Insert into product values (2,'cable')"}}; try { for(int i=0;i<(sizeof(dbobject)/sizeof(dbobject[0]));i++) { dbobjectIdea.OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobjectIdea.BeginTrans(); dbobjectIdea.ExecuteSQL(queryIdea); dbobjectIdea.CommitTrans(); cout<<"Query Execution Successful\n"; counter++; dbobjectIdea.Close(); } } catch (CDBException *pEx) { pEx->ReportError(); dbobject[counter].Close(); for(int j=counter-1;j>=0;j--) { dbobject[j].OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobject[j].BeginTrans(); int c = dbobject[j].Rollback(); dbobject[j].Close(); cout<<"SQL Query Rollbacked!\n"; } } } }; void main() { test t; t.testmethod(); }
-
hi,i am new to visual C++ programming. i have a simple visual C++ program that involves basic sql transactions and if a query fails then i have tried to rollback all the previous transactions as well. I am using MS SQL Server. the problem is that the rollback is not working in the tables in the server while when i run the program in the debug mode it shows that the rollback feature is working as it returns a positive integer from the rollback() function. the program shows no errors or warnings. Can anyone provide some suggestions? For testing i have used three queries in which the third one has been intentionally left wrong. I need the first two queries to be rollbacked when the error occurs in the third query. Would be glad if you can point out other errors as i am still learning C++ programming... Attached is the code:
class test { public: void testmethod() { CDatabase dbobject[100]; static int counter=0; char query [3][100]={{"Insert into product values (1,'keyboard')"},{"Insert into product values (2,'mouse')"},{"Insert into product values (2,'cable')"}}; try { for(int i=0;i<(sizeof(dbobject)/sizeof(dbobject[0]));i++) { dbobjectIdea.OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobjectIdea.BeginTrans(); dbobjectIdea.ExecuteSQL(queryIdea); dbobjectIdea.CommitTrans(); cout<<"Query Execution Successful\n"; counter++; dbobjectIdea.Close(); } } catch (CDBException *pEx) { pEx->ReportError(); dbobject[counter].Close(); for(int j=counter-1;j>=0;j--) { dbobject[j].OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobject[j].BeginTrans(); int c = dbobject[j].Rollback(); dbobject[j].Close(); cout<<"SQL Query Rollbacked!\n"; } } } }; void main() { test t; t.testmethod(); }
Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。
-
Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。
hi, i used the settings you told me. The problem is still there. The rollback does not remove the previous two entries. This time it also shows some 'Detected Memory Leaks!' error. What does that mean?
-
Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。
hey, thanks for your advice. i solved the problem myself. as you see in my code, i was using an array. i didnt need to use that. i used only a single object and treated the three transactions as a single transaction. it works now.!! - Moonis