我的数据库连接代码如下:
CString strConnection;
strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;")
_T("Data Source=note.mdb"));
m_DBCn.Open((LPCTSTR)strConnection); //打开程序数据库
m_Rs.SetDatabase(&m_DBCn);
m_Rs.Open(_T("select * from test;"));
其中变量的定义是:
CADORecordset m_Rs;
CADODatabase m_DBCn;
程序运行后出错:
错误如下:
   CADORecordset Error
          Code = 80004003
          Code meaning = 无效指针
          Source = (null)
          Description = (null)
请问错误在哪?

解决方案 »

  1.   

    给你个例子,肯定能帮助你。使用方便,而且简单,封装了ado。
    http://www.vckbase.com/document/viewdoc/?id=1557
      

  2.   

    可能是数据库打开未成功,note.mdb加上绝对路径试试.
      

  3.   


    select * from test;是不是多了一个分号?
      

  4.   

    对象初始了没有?DWORD CAdoOperator::InitAdo()
    {
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pConnection.CreateInstance(__uuidof(Connection));
    m_pCommand.CreateInstance(__uuidof(Command));
        try
    {
    //取得当前程序的完整路径,并拼出数据库的路径
    char szPath[MAX_PATH] = "" ;
    GetModuleFileName(NULL,szPath,MAX_PATH) ;
    PathAppend( szPath , _T("..\\iccard.mdb") ) ;
    CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" ;
    strConnection +=  szPath ;
    strConnection += ";Persist Security Info=True;Jet OLEDB:Database Password=hello" ;
    m_pConnection->Open((_bstr_t)strConnection,"","",-1) ;
    }
    catch(_com_error e)
    {
    return ERROR_DATABASECONNECT_FAILURE;
    }
    catch ( ... ) 
    {
    return ERROR_DATABASE_UNKNOWNERROR ;
    } return ERROR_DATABASE_SUCCESSED; 
    }
    /*
    该函数将ADO的错误进行格式化,并以CString 的形式返回错误信息。
    */
    CString CAdoOperator::GetErrorInfo()
    {
    CString ErrorMessage,temp; if( NULL == m_pConnection )
    {
    ErrorMessage = "错误的数据连接! ";
    return ErrorMessage ;
    }

    long errorcount=m_pConnection->GetErrors ()->GetCount ();
    _bstr_t add; for (short i=0;i<errorcount;i++)
    {
    add=m_pConnection->GetErrors ()->GetItem (_variant_t((short)i))->GetDescription ();
    temp = (char *)add;
    ErrorMessage +=temp;
    }
    return ErrorMessage;
    }
      

  5.   

    _RecordsetPtr m_pRecordset;
    _ConnectionPtr m_pConnection;
    AfxOleInit();
    CDialog::OnInitDialog();
    m_pConnection.CreateInstance(__uuidof(Connection));
    try                 
    {
    // 打开本地Access库Demo.mdb
    m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb","","",adModeUnknown);
    }
    catch(_com_error e)
    {
    MessageBox("数据库连接失败,确认数据库*.mdb是否在当前路径下!");
    return FALSE;


        m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pRecordset->CursorLocation = adUseClient;//需要排序的话必须要加上的
    try
    {
    m_pRecordset->Open("SELECT * FROM table",// 查询table中所有字段
    m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    } catch(_com_error e)
    {
    MessageBox("失败");
    return FALSE;
    }
    我的程序代码 给你参考一下
      

  6.   

    (1).在文件stdafx.h中最后一个#endif的前一行写入 
    #import "C:\program files\common files\System\ado\msado15.dll" no_namespace \ 
    rename("EOF","EndOfFile") \ 
    rename("LockTypeEnum","newLockTypeEnum")\ 
    rename("DataTypeEnum","newDataTypeEnum")\ 
    rename("FieldAttributeEnum","newFieldAttributeEnum")\ 
    rename("EditModeEnum","newEditModeEnum")\ 
    rename("RecordStatusEnum","newRecordStatusEnum")\ 
    rename("ParameterDirectionEnum","newParameterDirectionEnum") 
    如果你的系统不是安装在C盘的话就把#import 后面的C改成系统所有的盘 
    (2).在C***App类的 
    public:下加入 
    _RecordsetPtr m_pADOSet; 
    bool ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL); 
    在private:下加入_ConnectionPtr ADOConn; 
    在class C***App : public CWinApp 

    ... 
    };之后#endif之前加入extern C***App theApp; (3)在BOOL C***App::InitInstance()函数中Enable3dControls(); // Call this when linking to MFC statically这一行下面加入 
    if( FAILED(::CoInitialize(NULL)) ) 

    AfxMessageBox("ADO Init failed"); 
    return false; 

    try 

    ADOConn.CreateInstance(__uuidof(Connection)); 
    ADOConn->Open("DSN=OBDC数据源;Provider=MSDASQL","用户","密码", adConnectUnspecified);//这一行要自已修改 

    catch(_com_error &e) 

    CString err; 
    err.Format("%s", (char*)(e.Description()) ); 
    AfxMessageBox(err); 

    catch(...) 

    AfxMessageBox("Unknown Error..."); 

    m_pADOSet.CreateInstance(__uuidof(Recordset)); 
    并在文件最后加上如下代码: 
    bool C***App::ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL) 

    if ( ADOSet->State == adStateOpen) ADOSet->Close(); 
    try 

    ADOSet->Open(strSQL, ADOConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown); 
    return true; 

    catch(_com_error &e) 

    CString err; 
    err.Format("ADO Error: %s",(char*)e.Description()); 
    AfxMessageBox(err); 
    return false; 


    最后就可以在登录时执行SQL语句了,比如用户为CString strUser, 密码是CString strPwd;数据库表是user_table(user_id, user_name, user_pwd)则 
    _variant_t strQuery, Holder; 
    strQuery = "select * from user_table where user_name='"+strUser +"' and user_pwd='"+ strPwd +"'"; 
    theApp.ADOExecute(theApp.m_pADOSet, strQuery); 
    int iCount = theApp.m_pADOSet->GetRecordCount(); 
    if ( 0==iCount ) 

    AfxMessageBox(_T("密码错误"), MB_ICONEXCLAMATION); 
    return; 

    else 

    AfxMessageBox(_T("登录成功"), MB_ICONEXCLAMATION); 
    }
      

  7.   

    没初始化 COM
    在建立 connection 对象之前 调用CoInitialize(NULL);
    否则不能建立connection对象