try
   {
  UpdateData(true);
  m_adoConnection.BeginTrans();
       
 strSQL="insert into xs_xsddmx(ddbh,spbm,sl,dj,jqbj,bz) values('"+m_detaillist.GetItemText(i,0)+
          "','"+m_detaillist.GetItemText(i,1)+"',"+m_detaillist.GetItemText(i,6)+","+m_detaillist.GetItemText(i,5)+
  ",'"+m_detaillist.GetItemText(i,8)+"','"+m_detaillist.GetItemText(i,9)+"')";
        m_adoConnection.Execute(strSQL);
   
strSQL="insert into xs_xsddzb(ddbh,dhrq,ddyj,khbh,xsybh,ddje,czybh,shbj,zfbj,pjlx,bz) values('"+m_DDBH+
      "','"+m_DHRQ.Format("%Y-%m-%d")+"','"+m_DDYJSTR+"','"+m_KHBM+"','"+m_XSYBM+"',"+strHTJE+
  ",'"+m_CZYBM+"','"+m_SHBJ+"','"+m_ZFBJ+"','"+m_PJLXSTR+"','"+m_BZ+"')";
  m_adoConnection.Execute(strSQL);
  m_adoConnection.CommitTrans();
           MessageBox("数据保存成功");
  }
  catch (_com_error e)
  {
  TRACE(_T("Warning: 数据提交失败,请联系系统管理员"), e.ErrorMessage(), __FILE__, __LINE__);
  m_adoConnection.RollbackTrans();
  }
以上的程序采用ADO连接数据库,执行后,每次都提示保存成功,其实上面的xs_xsddmx表是不存在的,而xs_xsddzb表每次都插入一条记录,好象事务不起作用。同时请教一下,为什么SQL语句出现错误不出现任何提示呢,请高手帮我分析一下上面的代码

解决方案 »

  1.   

    要使用dbFailOnError选项。
    Given a syntactically correct SQL statement and proper permissions, the Execute member function will not fail even if not a single row can be modified or deleted. Therefore, always use the dbFailOnError option when using the Execute member function to run an update or delete query. This option causes MFC to throw an exception of type CDaoException and rolls back all successful changes if any of the records affected are locked and cannot be updated or deleted. Note that you can always call GetRecordsAffected to see how many records were affected.
      

  2.   

    老哥,小弟是初学者,能不能说清楚些,dbFailOnError如何使用呀
      

  3.   

    _variant_t records_affected;Conn1->Execute((LPCTSTR)sqlcmds,&records_affected,0);//VT_I4,lVal
    在Execute之后通过判断records_affected是否等于1知道insert执行结果
      

  4.   

    楼上所说好象不能解决事务所要求的,只知道insert执行结果有什么用,关键是如何执行错误发生后,事务如何回滚。
      

  5.   

    加多一个
    ////////////////////////////////////
    catch (...)
      {
      TRACE(_T("Warning: 数据提交失败,请联系系统管理员"), e.ErrorMessage(), __FILE__, __LINE__);
      m_adoConnection.RollbackTrans();
      }