问题集中在?????处,具体有以下两个疑问:
1。是不是重复使用了_RecordsetPtr::Open()函数,代码几乎相同
2. ColeCurrency类型的变量m_PAY怎样转换成货币型的(存进数据库表中)extern CHMISApp theApp;BOOL CMemZhenDlg::OnInitDialog() 
{
CDialog::OnInitDialog();/////////////////////////////////////以下从表中自动产生编号显示在EDIT中
try
{
m_pRs.CreateInstance("ADODB.Recordset");
m_pRs->Open("Select Patient_ID from Patient_Info Order by Patient_ID DESC",
theApp.m_pConn.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(m_pRs->adoEOF)
m_ID=1;
else
{
m_ID=m_pRs->GetCollect("Patient_ID").intVal;
m_ID++;
}
UpdateData(false);
m_pRs->Close();
        }

        catch(...)
{
AfxMessageBox("访问数据库时发生异常.\n请与系统管理员联系");
                return false;
} return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}
void CMemZhenDlg::OnMemZhenOK() 
{
// TODO: Add your control notification handler code here/////////////////////////////////////////////以下把记录插入表Patient_Info
try
{
UpdateData(true);
CString strID,strOLD;
strID.Format("%d",m_ID);
strOLD.Format("%d",m_OLD);

CString cmd="Insert Into Patient_Info Values(";
cmd+=strID+",";
cmd+="'"+m_NAME+"',";
cmd+="'"+m_SEX+"',";
cmd+=strOLD+",";
cmd+="'"+m_ADDRESS+"',";
cmd+="'"+m_TELPHONE+"',";
cmd+="'"+m_KESHI+"',";
cmd+="'"+m_METHOD+"')";
(theApp.m_pConn)->Execute((_bstr_t)cmd,NULL,adCmdText);////////////////////////////////以上运行正确,问题在下面代码中出现////////////////////////////////以下从表中自动产生编号,然后一起插入表Money
?????????? long m_mID;
                m_pRs->Open("Select Money_ID from Money Order by Money_ID DESC",
theApp.m_pConn.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(m_pRs->adoEOF)
m_mID=1;
else
{
m_mID=m_pRs->GetCollect("Money_ID").intVal;
m_mID++;
}
        m_pRs->Close();                CString strMID;
strMID.Format("%d",m_mID);
MessageBox(strMID); cmd.Empty();
                cmd="Insert Into Money Values(";
cmd+=strMID+",";
cmd+=strID+",";
cmd+="'"+m_NAME+"',";
????????        cmd+="'"+m_PAY+"',";
cmd+="'挂号费',";
cmd+="'"+theApp.m_dName+"',";
COleDateTime current;
current = COleDateTime::GetCurrentTime();
cmd+="'" + current.Format("%Y-%m-%d %H:%M:%S") + "')"; (theApp.m_pConn)->Execute((_bstr_t)cmd,NULL,adCmdText); }
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
catch(...)
{
    AfxMessageBox("访问数据库时发生异常.\n此次操作被取消.\n请与系统管理员联系");
    CDialog::OnOK();
}
}

解决方案 »

  1.   

    OnMemZhenOK() 这个函数在哪里执行的?
    如果在OnIni...之后就执行,那当然会出错。
    数据库不能打开二次的。要么你用二个实例来打开!…
    除m_pRs
    再在 OnMemZhenOK();里面再用一个m_pRs2来打开,这样应该好了!
      

  2.   

    OnMemZhenOK()在同一实现文件中执行谢谢二楼的大虾有没有具体解决办法
      

  3.   

    every recordset just  direct to a  table of db,
    close last recordset and open the next one,
    if work in the same recordset,you may use this way
    or use the function named Requery();
      

  4.   

    //CWinApp calss Member: 
       _ConnectionPtr m_pConnection;//Your Class:
    _RecordsetPtr m_pRecordset;//Member for to rs
    FunctionFilltoTarget(CString sTab, _RecordsetPtr rs)
    {
       CString str="SELECT TOP 10 * FROM "+sTab;
       try{
    rs.CreateInstance("ADODB.Recordset");
    rs->Open(_variant_t(str),_variant_t (IDispatch*)           theApp.m_pConnection
           ,true),adOpenForwardOnly, adLockReadOnly,adCmdText);   //....    rs->Close();
    rs=NULL;
       }catch(...)
       { 
          //...
        }
    }
      

  5.   

    非常简单,一个实例怎么当两个实便用呢!
    这样说吧,
    int i;
    你怎么能让 i 同时等于 5 和 10呢?
    只不过这里的 i 只是一个整型变量,而你现在用的是一个类的实例指针:m_pRs,是一样的道理!
    这样应该明白了吧!
      

  6.   

    那是不是把以前的值冲了,
    你想要用也可以了,就是先用 Close 关了,再开就可以了1
    道理是一样的!只不过一个是对象,一个是变量!!