只在INSERT INTO 这句的Execute语句中出现错误,而在上面的Selector * From中却没有错误,当我对这错误的部分用try...catch的时候,发生异常却不执行catch中的messageBox,百思不得其解,下面是我的代码
 打开数据库BOOL CFprint::OnOpenDataBase()
{
try
{
if(SUCCEEDED(m_pConn.CreateInstance(__uuidof(Connection))))
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Fingerprint.mdb","","",adModeUnknown);
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
} m_pRecs.CreateInstance(__uuidof(Recordset));
return TRUE;
}数据库操作部分
BOOL CFprint::OnWizardFinish() //属性页中的完成按钮的响应函数
{
//¸÷¸öÊôÐÔÒ³µÄÖ¸Õë
CBasic *Bsc = (CBasic *)m_pSheet->GetPage(0);
CExtend *Ext = (CExtend *)m_pSheet->GetPage(1);
CPhoto *Pho =(CPhoto *)m_pSheet->GetPage(2); CString strSql;
char strTem[100];
_variant_t RecordsAffected;
CWnd *pWndSave = GetDlgItem(ID_FPT_STA_SAVETIP); if(IDYES != MessageBox("ÊÇ·ñÈ·¶¨¿¼ÉúÐÅÏ¢ÎÞÎó£¬Íê³É±¾´ÎÌí¼Ó²Ù×÷?", "Ìáʾ", MB_YESNOCANCEL | MB_ICONQUESTION))
return FALSE;
//´ò¿ªÊý¾Ý¿â
if(!OnOpenDataBase())
{
MessageBox("Êý¾Ý¿â´ò¿ªÊ§°Ü£¬Êý¾Ý²»Äܱ£´æ", "´íÎó", MB_OK | MB_ICONERROR);
return FALSE;
} pWndSave->SetWindowText("±£´æ¿¼ÉúÐÅÏ¢");
m_prsSave.StepIt(); long m_Num = 0;
m_pRecs = m_pConn->Execute("SELECT * FROM AddInfo", &RecordsAffected, adCmdText);
while(!m_pRecs->adoEOF)
{
m_Num++;
m_pRecs->MoveNext();
}
if(!m_pRecs->BOF)
m_pRecs->MoveFirst(); strSql.Format("INSERT INTO AddInfo(TestPlace) VALUES ('%s')",Bsc->m_eTestPlace);
TRACE1(" DATAbase: %d ", GetLastError());
m_pConn->Execute((_bstr_t)strSql, &RecordsAffected, adCmdText);//错误行 m_prsSave.StepIt();

strSql.Format("UPDATE AddInfo SET FromTime1='%s',ToTime1='%s',Experience1='%s',Duty1='%s',Attestor1='%s',FromTime2='%s',\
 ToTime2='%s',Experience2='%s',Duty2='%s',Attestor2='%s',FromTime3='%s',ToTime3='%s',Experience3='%s',Duty3='%s',Attestor3='%s',\
 MemberName1='%s',Relation1='%s',WorkPlace1='%s',Phone1='%s',MemberName2='%s',Relation2='%s',WorkPlace2='%s',Phone2='%s',\
 MemberName3='%s',Relation3='%s',WorkPlace3='%s',Phone3='%s', FingerPrint=%d WHERE IDCard='%s'",Ext->m_strFDate1,\
 Ext->m_strTDate1,Ext->m_eExpA,Ext->m_eDutyA,Ext->m_eAttestorA,Ext->m_strFDate2,Ext->m_strTDate2,Ext->m_eExpB,Ext->m_eDutyB,\
 Ext->m_eAttestorB,Ext->m_strFDate3,Ext->m_strTDate3,Ext->m_eExpC,Ext->m_eDutyC,Ext->m_eAttestorC,Ext->m_eNameA,\
 Ext->m_eRelationA,Ext->m_ePlaceA,Ext->m_ePhoneA,Ext->m_eNameB,Ext->m_eRelationB,Ext->m_ePlaceB,Ext->m_ePhoneB,\
 Ext->m_eNameC,Ext->m_eRelationC,Ext->m_ePlaceC,Ext->m_ePhoneC,m_userAmount); m_pConn->Execute((_bstr_t)strSql, &RecordsAffected, adCmdText);
m_prsSave.StepIt(); memset(strTem, 0, 100);
strcpy(strTem,Pho->m_strNameTo);
Pho->m_strNameTo.Replace("TemPicture","Picture"); //copy the photo to the specified folder
if(CopyFile(strTem, Pho->m_strNameTo, FALSE))
{
memset(strTem, 0, 100);
GetCurrentDirectory(100, strTem);
strcat(strTem, "\\TemPicture\\*.*");
TRACE(strTem);
//Çå¿ÕÎļþ¼Ð
SHFILEOPSTRUCT shFile;
shFile.pFrom = strTem;
shFile.pTo = NULL;
shFile.wFunc = FO_DELETE;
shFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION ; SHFileOperation(&shFile);
}
else
{
MessageBox("ÕÕƬ±£´æʧ°Ü", "´íÎó", MB_OK | MB_ICONWARNING);
return FALSE;
}
AfxMessageBox("1");
//save the photo directory
strSql.Format("UPDATE AddInfo SET PhotoPath='%s' WHERE IDCard ='%s'", Pho->m_strNameTo, Bsc->m_eID);

m_pConn->Execute((_bstr_t)strSql, &RecordsAffected, adCmdText);
m_prsSave.StepIt();
MessageBox("¿¼ÉúÊý¾Ý±£´æÍê±Ï", "Ìáʾ", MB_OK | MB_ICONWARNING);

//¹Ø±ÕÊý¾Ý¿â
m_pConn->Close(); return CPropertyPage::OnWizardFinish();
}
请高手帮忙解决啊

解决方案 »

  1.   

    是catch(_com_error &e),不是catch(_com_error *e)
    try
    {
        //你的代码......
    }
    catch(_com_error &e)
    {
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());    // Print Com errors.
        TRACE(_T("Error\n"));
        TRACE(_T("\tCode = %08lx\n"), e.Error());
        TRACE(_T("\tErrorMessage = %s\n"), (LPCTSTR)e.ErrorMessage());
        TRACE(_T("\tSource = %s\n"), (LPCTSTR) bstrSource);
        TRACE(_T("\tDescription = %s\n"), (LPCTSTR) bstrDescription);
      

  2.   

    老大,我按你说的改了,但是仍是标题那个错误,因为错误行在INSERT INTO ……,Execute()这一行,而对于其他的Update Set ,Select *from都是没有错的,就insert into有错。请再考虑一下
      

  3.   

    数据已经存在的时候插入是有错误的,有的字段值插入的时候没有“Allow NULL”属性的话也会出错,所以必须把这类字段的默认值自己写一起insert into到数据库中。
      

  4.   

    strSql不要用CString , 用char strSql[128]试试。
      

  5.   

    to zyq5745:
      你说的是主键为空吧,是的,我确实犯了这个错误,经过改正后,运行正常了,谢谢,接分。to Un1:
      我感觉你说的这个对我的大量、有变化的数据来说不适合,我只能使用CString,谢谢你的回答,来者有分。