_variant_t name;
  m_rs.CreateInstance("adodb.recordset");
  CString s;
  s.Format("select * from worker");
  m_rs->Open(s,m_pConnection,3,3,0);
  name=m_rs->GetCollect("用户名");
  s.Format("%s",name.bstrVal);
  m_username=s;
  UpdateData(false);
为什么提示'm_pConnection':undeclared identifier

解决方案 »

  1.   

    前面加上
    m_pConnection.CreateInstance("adodb.Connection")
    m_pConnection.Open() '具体open什么,自己去查书,不同的数据库这里要写的不同
      

  2.   

    BOOL CAsdApp::InitInstance()
    {
    AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.
    AfxOleInit();///初始化COM库
    ////////////连接数据库//////////////
    HRESULT hr;
    try
    {
    hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
    if(SUCCEEDED(hr))
    {
    hr = m_pConnection->Open("Data Source=moto","","",adModeUnknown);///连接数据库
    ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
    }
    }
    catch(_com_error e)///捕捉异常
    {
    CString errormessage;
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    return FALSE;

    #ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CAsdDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }
    在APP中已经定义了m_pConnection
      

  3.   

    m_pConnection他不是系统已经定义的变量你必须定义后才能使用
      

  4.   

    …………
    要保证m_rs->Open(s,m_pConnection,3,3,0);的语句能够访问m_pConnection
    (你的m_pConnection是定义在那里?)找找变量作用域方面有没有问题
      

  5.   


    m_pConnection.CreateInstance("ADODB.Connection");
    try
      {
    m_pConnection->Open("Provider=Microsoft Jet 4.0 OLE DB Provider;Data Source=e:\\lgmq.mdb",
       "","",adModeUnknown);
    }
      }
      catch(_com_error &e)///捕捉异常
      {
      CString errormessage;
      errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
      AfxMessageBox(errormessage);///显示错误信息
      } 
       pRst.CreateInstance("ADODB.Recordset");
       pRst->CursorLocation=adUseClient;  
       try
    {

    pRst->Open("SELECT * FROM 班报",                 m_pConnection.GetInterfacePtr(),   adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    }
    catch(_com_error * e)
    {
    AfxMessageBox(e->ErrorMessage());
    }
        m_gridban1.SetCaption("班数据");
           m_gridban1.SetRefDataSource(NULL);
           m_gridban1.SetRefDataSource( (LPUNKNOWN) pRst );
    }
      

  6.   

    除以上代码外,还需要在头文件中定义:
    public:
    _pConnection m_pConnection;
    _pRecordset  pRst;
      

  7.   

    你的m_pConnection 在CAsdApp类中定义的,
    但你的open 不一定在CAsdApp 中,要么你在同一类中定义和使用
    要么定义不同的指针,进行传递如下:
     如:xxx类中定义:
    public:
       _ConnectionPtr m_pConnection;
       _RecorsetPtr rs;
       在CAsdApp类调用xxx类时
       xxx->m_pConnection=m_pConnection;
      

  8.   

    好了
    本人将m_pConnection移到与m_rs一起后解决问题
    多谢各位
    但新问题又来了
    HRESULT hr;
    try
    {
    hr=m_pConnection.CreateInstance("adodb.connection");
    if(SUCCEEDED(hr))
    {
    hr=m_pConnection->Open("data source=moto","","",0);
    }
    }
    catch(_com_error e)
    {
    CString errormessage;
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    return FALSE;
    }
    // TODO: Add extra initialization here

      _variant_t name;
      try
      {
      hr=m_rs.CreateInstance("adodb.recordset");
      if(SUCCEEDED(hr))
      {
      hr=m_rs->Open("select * from worker",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
      }
      }
      catch(_com_error e)
      {
    CString errormessage;
    errormessage.Format("打开数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    return FALSE;
      }
      try
      {
      m_rs->MoveFirst();
      name=m_rs->GetCollect("用户名");
      }
      catch(_com_error e)
      {
    CString errormessage;
    errormessage.Format("获取数据失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    return FALSE;
      }
    为什么提示
    获取数据失败
    错误信息:指针无效
      

  9.   

    m_rs与m_pConnection 没有关联,
      好象recordsetPtr 的ActiveConnection 属性为指定连接指针
      

  10.   

    怎么上面都说了那么多了啊?呵呵~~~~~我还有没有分赚啊?嘿嘿~~~要先定义一个_variant_t对象和当前的Connection关联起来!要不你怎么查哦?
      

  11.   

    新问题我来答!hr=m_pConnection->Open("data source=moto","","",0);1.data source=moto 这个数据源是否合法?2.你和RecordSet对象与Connectio对象建立连接了吗?
      

  12.   

    要这样查询才可以,呵呵~~至少我是这样做的!
    _variant_t vConn; try
    {
    if(m_pConn->State != adStateOpen)
    {
    return FALSE;
    } if(m_pRecordset->State != adStateClosed)
    {
    m_pRecordset->Close();
    } vConn = (IDispatch *)m_pConn;//Connection对象指针来构造一个_variant_t对象
    m_pRecordset->Open(lpszSQL,vConn,adOpenStatic,adLockOptimistic,adCmdText); return TRUE;
    }