我想利用ADO对象把数据库中字段在DBGRID中显示,我不想用ADO控件。
如何在程序中动态设置ADO对象和DBGRID控件绑定!!谢谢

解决方案 »

  1.   

    // set datasource of DBGrid dynamically

    void CDgDlg::OnOK() 
    { //notes : call AfxOleInit in CXXApp::InitInstance()
    // I also add a line in stdafx.h 
    //#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
       try
       {
          _ConnectionPtr pConn("ADODB.Connection");
          _RecordsetPtr  pRst("ADODB.Recordset");
          pConn->Open("Provider=sqloledb;Data Source=Dell1;"
             "Initial Catalog=testdb;User Id=sa;Password=;",
             "", "", adConnectUnspecified);
          pRst->Open(
             "table1",
             _variant_t((IDispatch *) pConn, true),
             adOpenStatic,
             adLockReadOnly,
             adCmdTable);
      //CDataGrid m_grid; is a member of CDgDlg.
    m_grid.SetRefDataSource(pRst.Detach());
       }
       catch (_com_error &e)
       {
      char mybuf[10240];
          wsprintf(mybuf,"Description = '%s'\n", (char*) e.Description());
      AfxMessageBox(mybuf);
       }
    }
      

  2.   

    SAMPLE: AdoDataGrid.exe Demonstrates How to Use ADO with DataGrid Control Using Visual C++ Q229029
    --------------------------------------------------------------------------------
    The information in this article applies to:ActiveX Data Objects (ADO), versions 2.0, 2.1, 2.5, 2.6 
    Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0