我在用VC++6.0开发oraclet数据库时,我使用_ConnectionPtr对象的Execute方法直接执行SQL中insert语句时,如果不放在循环中则不会出错,一但放入循环操作中,总是不能执行,因为我有多个数据要执行insert,所以必须放入循环中,但在就卡在这里了...紧急求助

解决方案 »

  1.   

    把你的那段循环程序公开。讨论就可以深入了。  我觉得是insert更新有问题
      

  2.   

    但我把循环拿掉后却能正常插入数据,insert语句肯定没有问题的,我已对这问题想了一天,而且经过测试,只要加上循环就会被catch到,即使循环只执行一次也是这样的
      

  3.   

    #define M_BM 2
    #define M_SL 4
    #define M_DJ 5
    #define M_GYS 6
    #define M_MAKE_DATE 7
    #define M_EFFI_DATE 8void CAddlib::OnButtonOk() 
    {
    /**********************从列表控件中得到各种药品编码及数据 价格 数量 生产日期 有效日期********/
    // int ItemCount;
    UpdateData();
    if(!(m_medCount=m_med.GetItemCount())){
    AfxMessageBox("您没有进行任何入库操作......");
    return;
    }
    CString strBm,strSl,strDj,strGys,strDatemake,strDateeffi,strSql,t1,t2,t3,t4;
    // strTemp.Format("当前的记录总数为:%d",m_medCount);
    // AfxMessageBox(strTemp);
    BSTR strAttribute;
    _variant_t Temp1,Temp2,Temp3;
    VARIANT Recordaffect;
    unsigned j=0;
    /*******************************************************************************************/

    try{
    m_gys.GetWindowText(strTemp);
    if(strTemp.IsEmpty()){
    AfxMessageBox("空的供应商");
    return;
    }
    strSql.Format("select gongbm from codegys where gongmc=\'%s\' ",strTemp);
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 
    if(recSet->adoEOF){
    AfxMessageBox("供应商不在表中,请在表中先添加供应商品");
    recSet->Close();
    return;
    }
    Temp1=recSet->GetCollect("gongbm");
    recSet->Close();
    /***********************************************************************************************/
    m_rybm.GetWindowText(strTemp);
    if(strTemp.IsEmpty()){
    AfxMessageBox("空的业务员");
    return;
    }
    m_rybm.GetWindowText(strTemp);
    strSql.Format("select rybm from operator where name=\'%s\' ",strTemp);
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 
    if(recSet->adoEOF){
    AfxMessageBox("业务员不在表中,请在表中先添加");
    recSet->Close();
    return;
    }
    Temp2=recSet->GetCollect("rybm");
    recSet->Close();

    /**********************************************************************************************/

    strSql.Format("select rybm from operator where name=\'%s\' ",g_data.LogonName);
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 
    if(recSet->adoEOF){
    AfxMessageBox("登录人员不在表中,请在表中先添加");
    recSet->Close();
    return;
    }
    Temp3=recSet->GetCollect("rybm");
    recSet->Close();
    /*********************************************************************************************/
    t1=(char*)_bstr_t(Temp1);  //供应商编码
    t2=(char*)_bstr_t(Temp2); //业务员编码
    t3=(char*)_bstr_t(Temp3); //登录人员编码

    /********************在ITEMRKTOU中插入新的入库单项目************************************************/


    m_rybm.GetWindowText(strTemp);
    strSql.Format("insert into itemrktou(danjuhao,piaojuhao,riqi,gongbm,kfbm,业务员,\
    业务员名,入库员,入库员名) values(\'%s\',\'%s\',to_date(\'%s\',\'yyyy/mm/dd\'), \'%s\',\'%s\',\'%s\'\
     ,\'%s\',\'%s\',\'%s\') ",m_rktou,m_piao,m_date.Format("%Y/%m/%d"),t1,"100",t2,\
     strTemp,t3,g_data.LogonName); 
    strAttribute=strSql.AllocSysString();
    HospitalConnect->Execute(strAttribute,&Recordaffect,adCmdText); 
    // HospitalConnect->Execute("insert into itemrktou(piaojuhao) values(\'22222\') ",&Recordaffect,adCmdText); 
    // HospitalConnect->Close();




    // while(j<m_medCount){ //只要这儿加上这个循环,运行过程中将产生错误,而拿掉就能一切正常

    strBm=m_med.GetItemText(j,M_BM);   //药品编码
    strSl=m_med.GetItemText(j,M_SL); //药品数量
    strDj=m_med.GetItemText(j,M_DJ); //药品单价
    strGys=m_med.GetItemText(j,M_GYS);     //供应商  
    strDatemake=m_med.GetItemText(j,M_MAKE_DATE);  //生产日期
    strDateeffi=m_med.GetItemText(j,M_EFFI_DATE);  //有效日期

    if(!strGys.IsEmpty()){
    strSql.Format("select gongbm from codegys where gongmc=\'%s\' ",strGys);
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 
    if(recSet->adoEOF)
    {
    AfxMessageBox("数据库出现异常... in codegys");
    recSet->Close();
    t1="";
    }
    else{
    Temp1=recSet->GetCollect("gongbm");  //要转化成CHAR* 
    recSet->Close();
    t1=(char*)_bstr_t(Temp1);
    }
    }
    else t1="";


    strSql.Format("select id from medlie where sl=0 and kfbm=100 and  ypbm=\'%s\'  ",strBm);
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 

    if(recSet->adoEOF){
    recSet->Close();
    //新生成一个ID编码,然后将所有数据插入到其中
    strSql.Format("select max(id) as id from medlie ");
    strAttribute=strSql.AllocSysString();
    recSet->Open(strAttribute,(IDispatch*)HospitalConnect,adOpenDynamic,adLockOptimistic,adCmdText); 
    Temp2=recSet->GetCollect("ID");
    t4=(char*)_bstr_t(Temp2);
    recSet->Close();
    strSql.Format("insert into medlie(id,kfbm,ypbm,factorycode,rq1,rq2,sl,jinjia,danjuhao) \
     values(to_number(\'%s\',\'99999999\')+1,\'100\',\'%s\',\'%s\',to_date(\'%s\','yyyy/mm/dd'),\
     to_date(\'%s\','yyyy/mm/dd'),\'%s\',to_number(\'%s\',\'9999.99\'),\'%s\') ",\
     t4,strBm,t1,strDatemake,strDateeffi,strSl,strDj,m_rktou);
    strAttribute=strSql.AllocSysString();
    HospitalConnect->Execute(strAttribute,&Recordaffect,adCmdText); 
    }
    else{
    Temp2=recSet->GetCollect("ID");
    strTemp=(char*)_bstr_t(Temp2);
    recSet->Close();
    strSql.Format("update medlie set factorycode=\'%s\',rq1=to_date(\'%s\',\'yyyy/mm/dd\'),rq2=to_date(\'%s\',\'yyyy/mm/dd\'),\
    sl=\'%s\',jinjia=to_number(\'%s\','9999.99'),danjuhao=\'%s\' where id=to_number(\'%s\',\'9999999\')",t1,strDatemake,strDateeffi,\
    strSl,strDj,m_rktou,strTemp);
    strAttribute=strSql.AllocSysString();
    HospitalConnect->Execute(strAttribute,&Recordaffect,adCmdText); 
    } // j++;}

    }

    catch(_com_error e)
    {
    AfxMessageBox("插入数据到itemrktou 时出现错误,请检查数据库系统有无问题");
    AfxMessageBox(strSql);
    AfxMessageBox(e.ErrorMessage());
    return ;
    }
    /*******************************************************************************************/

    AfxMessageBox("当前数据入库成功......");

    OnOK();}
      

  4.   

    没有看你的代码,给你几个Debug的建议:1、在INSERT出错时将SQL语句记LOG,然后在ORACLE中直接执行这条SQL语句,看看会不会出错2、INSERT出错时查看返回错误代码,它会告诉你为什么出错。