try
{
m_pRecordset->Open("SELECT * FROM users",      
theApp.m_pConnection.GetInterfacePtr(),// 请问那个theApp是干麻的?为什么这代码
在事例中可以运行,而我复制这段代码后无法运行?
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} try
{
m_pRecordset->Open("SELECT * FROM users",      // 查询DemoTable表中所有字段
theApp.m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}      _variant_t var;
CString strName,strAge;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
} // 读入库中各字段并加入列表框中
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var); m_AccessList.AddString( strName + " --> "+strAge ); m_pRecordset->MoveNext();
} m_AccessList.SetCurSel(0);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}     
}错误内容:
F:\vc\myapp\mdb5\mdb5Dlg.cpp(185) : error C2065: 'theApp' : undeclared identifier
F:\vc\myapp\mdb5\mdb5Dlg.cpp(185) : error C2228: left of '.m_pConnection' must have class/struct/union type
F:\vc\myapp\mdb5\mdb5Dlg.cpp(185) : error C2228: left of '.GetInterfacePtr' must have class/struct/union type
===============
请问那个theApp是干麻的?为什么这代码在vc知识库(直接通过的AOD访问数据库)事例代码中可以运行,而我复制这段代码后无法运行?

解决方案 »

  1.   

    要在你的项目的头文件(XXX.h)中加下面这句
    extern CXXXApp theApp;
    XXX为你的项目名
      

  2.   

    还是不懂,呵呵。
    我的项目名为:mdb5
    我在mdb5dlg.h中添加 extern cmdb5App theApp;
    显示:
    f:\vc\myapp\mdb5\mdb5dlg.h(14) : error C2146: syntax error : missing ';' before identifier 'theApp'
    f:\vc\myapp\mdb5\mdb5dlg.h(14) : fatal error C1004: unexpected end of file found
    Generating Code...
    Error executing cl.exe.
    mdb5.exe - 4 error(s), 0 warning(s)
    我能不能吧原代码发上来,你帮我看一下那的问题?
      

  3.   

    1.stdafx.h中加入
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") 
    2。app文件的InitInstance中加入
    if(S_OK!=OleInitialize(NULL))
    {
    AfxMessageBox("初始化COM组件库错误");
    }
    下面就是数据库操作
    _ConnectionPtr pConn;
    _RecordsetPtr  pRs;
    CString strSQL;
    pConn.CreateInstance(__uuidof(Connection));
    pConn->CursorLocation=adUseClient;
    strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
    pConn->Open(_bstr_t(strSQL),"","",-1);

    pRs.CreateInstance(__uuidof(Recordset));
    pRs->CursorLocation=adUseClient;
    pRs->PutActiveConnection(pConn.GetInterfacePtr());

    pRs1.CreateInstance(__uuidof(Recordset));
    pRs1->CursorLocation=adUseClient;
    pRs1->PutActiveConnection(pConn.GetInterfacePtr()); strSQL="select * from catalog order by cataid";
    pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
      

  4.   

    1.stdafx.h中加入
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") 
    2。app文件的InitInstance中加入
    if(S_OK!=OleInitialize(NULL))
    {
    AfxMessageBox("初始化COM组件库错误");
    }
    ================
    上面两行我已经加入过,还有我连接的是access,不是sqlserver.
    请问该怎么办?
      

  5.   

    ///////////////
    extern CMdb5App theApp;       // 在此引用应用类中的theApp来获取库连接指针
    ///////////////////////////////
    我在mdb5dlg.cpp文件中加入上面一句话后,能编译成功,但不能运行?请问是什么原因?加在下面句的后面
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    ///////////////////////////
    这个是我错误工程的原代码,没加入msado15.dll这个文件。那位好朋友帮我看一下错误在那?
    http://www.vchelp.net/ASP/cdf_pic/200410/subject_1_119572.rar
      

  6.   

    看过了,你只要在mdb5Dlg.cpp最前面加extern CMdb5App theApp;就可以了吧。
      

  7.   

    好象还是不行呢,我已经在mdb5Dlg.cpp最前面加extern CMdb5App theApp;
    但也是能编译成功,但不能运行!
      

  8.   

    已经帮你改过能运行了,你在StdAfx.h中加入#import "c:\program files\common files\system\ado\msado15.dll" \
    no_namespace \
    rename ("EOF", "adoEOF")  
    不要用#include <msadl15.dll>
    然后再.cpp加extern CMdb5App theApp;就可以了
      

  9.   

    我没用#include <msadl15.dll>
    我用的是#import "msado15.dll" no_namespace rename ("EOF", "adoEOF")  
    我的msado15.dll放在项目文件下。
    extern CMdb5App theApp;我也加入了,问题依旧。
    要不把你编译成功的文件打包传给我?
      

  10.   

    我的QQ为79288652,你用qq传给我行吗?谢谢!
    这是我那边错误的图片。
    http://asp5.6to23.com/scm110/test/error.gif
      

  11.   

    要不在vchelp上我也问过,你在上面上传也行。
    http://www.vchelp.net/cndevforum/subject_view.asp?subject_id=119572&forum_id=79
      

  12.   

    我把这个.dll放在工程目录里面然后#import "msado15.dll" no_namespace rename("EOF","adoEOF")
    也是可以编译成功的。
    晕,我总于看到你哪里出错了,你的
    m_pRecordset->Open("SELECT * FROM user",      theApp.m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    里面没有“name”和“age”字段,当然运行到读数据表的时候就错了啊。
    你改成
    m_pRecordset->Open("SELECT * FROM Demotable",                // 查询DemoTable表中所有字段
    theApp.m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    就可以了,“name”和“age”字段在你的Demotable表。
    hoho,你复制粘贴的时候也太大意了。
      

  13.   

    还有什么问题,发信给我好了,[email protected]
      

  14.   

    多谢,多谢!
    是我太粗心了!也许是因为我的mdb里面有两个表格我都被他们搞糊涂了-_-,呵呵。
    以后有问题想你继续向你请教:-)