我用VC6.0 MFC打开了ODBC的数据库,也读取了表名,怎样来读起表里的数据 

解决方案 »

  1.   

    str是赋的表名strSQL = ("SELECT * FROM ") + str;
         m_rs.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL,CRecordset::readOnly);
    编译通过运行时报错,列没被绑定
      

  2.   

    str是赋的表名strSQL = ("SELECT * FROM ") + str;
         m_rs.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL,CRecordset::readOnly);
    编译通过运行时报错,列没被绑定
      

  3.   

    一般我是recordset = connection去Execute(sql语句),然后返回结果。
    比如:
    m_rs = connection对象.Execute((_bstr_t)strSQL, ..., adCmdText);
      

  4.   

    我是用的ODBC连接的数据库,CRecordset m_rs;
    strSQL = ("SELECT * FROM ") + str;
    m_rs = m_db.ExecuteSQL((_bstr_t)strSQL);
    error C2582: 'CRecordset' : 'operator =' function is unavailable
    Error executing cl.exe.
      

  5.   


    看看下面的代码可以参考不:_ConnectionPtr m_pConnection;
    _RecordsetPtr m_pRecordset;BOOL CWanGuanDialog::OnInitDialog()
    {
    CDialog::OnInitDialog();
    CString szPath; 
        GetModuleFileName(NULL,szPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH); 
        szPath.ReleaseBuffer (); 
        int nPos; 
        nPos = szPath.ReverseFind ('\\'); 
        szPath = szPath.Left (nPos); 
    szPath = szPath + "\\WanGuan";
        CString szFile = szPath + "\\PSTAD2003.mdb"; 
        CString szAtr; 
        szAtr.Format(_T("DSN=PSTAD2003!DBQ=%s!DEFAULTDIR=%s!!"),szFile,szPath);
    int nlen; 
    nlen = szAtr.GetLength(); 
    for(int i = 0;i <nlen;i++) 

    if(szAtr.GetAt(i) == '!') 
    szAtr.SetAt (i,'\0') ; 

    if (FALSE == SQLConfigDataSource(NULL, ODBC_ADD_DSN, _T("Microsoft Access Driver (*.mdb)\0"), (LPCWSTR)szAtr)) 
    AfxMessageBox(_T("数据源配置失败,确认数据库PSTAD2003.mdb是否在当前路径下!"));  AfxOleInit();
        m_pConnection.CreateInstance(_uuidof(Connection));
        m_pRecordset.CreateInstance(_uuidof(Recordset));
    try                 
    {
    m_pConnection->Open("DSN=PSTAD2003","","",0);//连接数据库
    }
    catch(_com_error e)
    {
    AfxMessageBox(_T("数据库连接失败,确认数据库PSTAD2003.mdb是否在当前路径下!"));
    return FALSE;
    }     
    try
    {
    CString strSql=_T("select * from PSTAD2003");
    m_pRecordset->Open((LPCTSTR)strSql,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
    }
    catch(_com_error *e)
    {
    AfxMessageBox(e->ErrorMessage());
    }
    try
    {
    if(!m_pRecordset->BOF)
    m_pRecordset->MoveFirst();
    else
    {
    AfxMessageBox(_T("表内数据为空"));
    return FALSE;
    }
    while(!m_pRecordset->AdoEOF)
    {
    _variant_t var = m_pRecordset->GetCollect("外径");
    if(var.vt != VT_NULL)
    m_OD = (LPCSTR)_bstr_t(var);
    m_COMBO.AddString((LPCTSTR)m_OD);
    m_pRecordset->MoveNext();
    }
    }
    catch(_com_error *e)
    {
    AfxMessageBox(e->ErrorMessage());

    return TRUE;  // return TRUE  unless you set the focus to a control
    }