SQL transaction rollback problem
- From: Moonis Ahmed <Moonis Ahmed@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 21 May 2007 03:01:02 -0700
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();
}
.
- Follow-Ups:
- Re: SQL transaction rollback problem
- From: Mary Chipman [MSFT]
- Re: SQL transaction rollback problem
- Prev by Date: Re: ODBC Connection in Deployement of Integration Services Project
- Next by Date: Re: SQL transaction rollback problem
- Previous by thread: Re: ODBC Connection in Deployement of Integration Services Project
- Next by thread: Re: SQL transaction rollback problem
- Index(es):