VC下使用数据库,用哪种方式可以不通过控件,直接通过对象实现的?使用CRecordset好象必须绑定控件,不知道DAO,OLE DB,ADO等能否不用控件直接读数据源。 求教!先谢了

解决方案 »

  1.   

    ADO
    不用控件,有几个步骤
      

  2.   

    // 包含库文件
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")// 初始化COM支持
    CoInitialize(NULL);// 使用Connection和Recordset对象
    _ConnectionPtr pConnect;
    _RecordsetPtr pRecordset;
    // 打开Connection,连接SQLServer2K
    try{
    pConnect.CreateInstance(_uuidof(Connection));
    _bstr_t bstrCon = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;
    Initial Catalog=mydatabase;Data Source=(local)";
    pConnect->Open(bstrCon, "","",adoModeUnknown);
    }
    catch(_com_error e){
    AfxMessageBox(e.ErrorMessage());
    }
    // 打开Recordset,通过Connection连接取得记录集
    try{
    pRecordset.CreateInstance(_uuidof(Recordset));
    _bstr_t bstrSQL = "Select * from TableName";
    pRecordset->Open(bstrSQL, pConnect.GetInterfacePtr(),
    adOpenDynamic, adLockOptimistic, adCmdTable);
    }
    catch(_com_error e){
    AfxMessageBox(e.ErrorMessage());
    }
    // 遍历记录集
    try{
    pRecordset->MoveFirst();
    while(pRecordset->adoEOF != VARIANT_TRUE)
    {
    // 读字段
    CString str = (char*)(_bstr_t)pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value;
    // 写字段
    pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value = _bstr_t("无名氏");
    pRecordset->Update();
    pRecordset->MoveNext();
    }
    }
    catch(_com_error e)
    { AfxMessageBox(e.ErrorMessage()); }
      

  3.   

    谢谢楼上的!我也正在研究ADO,但发现你讲的步骤比书上讲的简单很多
      

  4.   

    以前总结的嘛,相当于一个备忘。至于细节,诸如函数参数意义,你得看书和MSDN了。
      

  5.   

    CRecordset不需要,
    CRecordset devSet(&PMAIN->m_database);//PMAIN->m_database定义好的CDatabase对象
    CString strDevId;
    strDevId.Format("%d", PMAIN->m_ComDev.comData[i].dev_id);
    devSet.m_strFilter = "a.dev_tp_id = b.dev_tp_id and a.dev_id = " + strDevId;
    devSet.Open(CRecordset::snapshot, _T("select dev_tp_nm, dev_id from t_device a, t_dev_type b"), CRecordset::readOnly);
        if(devSet.GetRecordCount() <= 0)
    {
               AfxMessageBox("δÕÒµ½¸ÃÉ豸¶ÔÓ¦µÄÉ豸ÀàÐÍÐÅÏ¢!");
    }
      

  6.   

    楼上的还是需要一个dialog吧?我是希望直接在SDI窗口上完成