_variant_t Ra;
..............
try
{
g_pCn->BeginTrans();
g_pCn->Execute(updateStr1,&Ra,1);
g_pCn->Execute(updateStr2,&Ra,1);
}
catch
{
MessageBox("failed","Info");
g_pCn->Rollback();
return;
}
MessageBox("ok","Info");
g_pCn->CommitTrans();
这样不行吗?为什么在执行g_pCn->BeginTrans()的时候就会抛出异常?
但执行第二遍的时候就可以了?
大伙儿帮帮我,谢谢了!

解决方案 »

  1.   

    执行顺序错误了,应该也要捕捉提交事务g_pCn->CommitTrans();时发生的异常,因为数据库的操作这时才发生。
    try
    {
    g_pCn->BeginTrans();
    g_pCn->Execute(updateStr1,&Ra,1);
    g_pCn->Execute(updateStr2,&Ra,1);
             g_pCn->CommitTrans(); //应该放在这儿
    }
    catch(...)
    {
    MessageBox("failed","Info");
    g_pCn->Rollback();
    return;
    }
    MessageBox("ok","Info");
      

  2.   

    Note   Not all providers support transactions. Verify that the provider-defined property "Transaction DDL" appears in the Connection object's Properties collection, indicating that the provider supports transactions. If the provider does not support transactions, calling one of these methods will return an error.
    (MSDN 2001 oct.)
      

  3.   

    CommitTrans放到try中
    不过有时异常还是要捕获的
      

  4.   

    那么rollback错误是否也要Catch?