我刚学习vc下的数据库操作.我照葫芦画瓢的研究别人的入门程序.首先在
程序的InitInstance()方法下加入数据连接的部分,
AfxOleInit();///初始化COM库
////////////连接数据库////////////// m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
try
{
 m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",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;
}
然后编译没问题,然后下一步继续抓代码,我在窗口的OnInitDialog()里面加了如下代码:
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",                // 查询DemoTable表中所有字段
theApp.m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
好像是打开数据库执行sql语句,这时候就出现错误了.错误如下:
E:\Program Files\Microsoft Visual Studio\MyProjects\db\dbDlg.cpp(123) : error C2065: 'theApp' : undeclared identifier
E:\Program Files\Microsoft Visual Studio\MyProjects\db\dbDlg.cpp(123) : error C2228: left of '.m_pConnection' must have class/struct/union type
E:\Program Files\Microsoft Visual Studio\MyProjects\db\dbDlg.cpp(123) : error C2228: left of '.GetInterfacePtr' must have class/struct/union type
发现程序对
theApp.m_pConnection.GetInterfacePtr(),  这行不识别.
theApp不是在CXXApp里面有了吗?我找到了,是这行
CDbApp theApp;那为什么还会有上面的提示呢,怎么解决,谢谢各位帮忙.

解决方案 »

  1.   

    你没include ado需要的库吧!
      

  2.   

    如在你的stdafx里写#import "C:\\program files\\common files\\system\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")
    并且在
    initinstance里::CoInitialize(NULL);
      

  3.   

    当然include ado的库了.还是有啊.楼上的::CoInitialize(NULL);我在别人的示例程序里没看到啊,放到什么位置?我随便放到initinstance里的任何位置,还是有上面的错误.