问题如题所述,大家能否提供下例子,我看了书上的例子,但运行不出来,我刚学数据库,对这方面还不熟,想找个例子来比较下。

解决方案 »

  1.   

    我程序中的一部分,不知道对你有没有用void CMainView::UpdateDatabase()
    {
    _RecordsetPtr pRecordset;
    pRecordset.CreateInstance(__uuidof(Recordset));
    try
    {
    pRecordset->Open("SELECT * FROM bbs",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
    }
    catch(_com_error e)
    {
    AfxMessageBox(e.ErrorMessage());
    exit(0);
    }
    CString strTemp;
    m_wndList.DeleteAllItems();
    _variant_t var;
    try
    {
    if(pRecordset->BOF)
    AfxMessageBox("数据库为空!");
    pRecordset->MoveLast();
    while(!pRecordset->BOF)
    {
    var=pRecordset->GetCollect("Id");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="";
    int nRow=m_wndList.InsertItem(0,strTemp);

    var=pRecordset->GetCollect("Url");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="";
    m_wndList.SetItemText(nRow,1,strTemp);

    var=pRecordset->GetCollect("Type");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="";
    m_wndList.SetItemText(nRow,2,strTemp);

    var=pRecordset->GetCollect("UserName");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="未设置";
    m_wndList.SetItemText(nRow,3,strTemp);

    var=pRecordset->GetCollect("PassWord");
    if(var.vt != VT_NULL) strTemp = "已设置";
    else strTemp="未设置";
    m_wndList.SetItemText(nRow,4,strTemp);

    var=pRecordset->GetCollect("MaxUiD");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="自动判断";
    if(strTemp=="0") strTemp="自动判断";
    m_wndList.SetItemText(nRow,5,strTemp);

    var=pRecordset->GetCollect("Interval");
    if(var.vt != VT_NULL) strTemp = (LPCSTR)_bstr_t(var);
    else strTemp="自动判断";
    if(strTemp=="0") strTemp="自动判断";
    m_wndList.SetItemText(nRow,6,strTemp);

    var=pRecordset->GetCollect("IsChosen");
    if(var.intVal==1)
    m_wndList.SetItemText(nRow,7,"√");
    else
    m_wndList.SetItemText(nRow,7,"×");
    pRecordset->MovePrevious();
    }
    }
    catch(_com_error e)
    {
    AfxMessageBox(e.ErrorMessage());
    }
    if(pRecordset->State)
    pRecordset->Close();
    pRecordset=NULL;
    }
      

  2.   

            if(pRecordset->BOF)
                AfxMessageBox("数据库为空!");后加return;
     
    _ConnectionPtr m_pConnection是C**App的成员变量BOOL CQunFaApp::InitInstance()
    { m_pConnection.CreateInstance(__uuidof(Connection));
    try
    {
    m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb","","",adModeUnknown);
    }
    catch(_com_error e)
    {
    AfxMessageBox("数据库连接失败!");
    exit(0);
    }
    }