郁闷 SDI里使用ADO,_ConnectionPtr m_pConnection编译出错!我觉得代码没错:第一步:// Exclude rarely-use
郁闷 SDI里使用ADO,_ConnectionPtr m_pConnection;编译出错!
我觉得代码没错:
第一步:
// Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h>// MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")//连接数据库
第二步:
BOOL CEX2App::InitInstance()
{
AfxEnableControlContainer();
AfxOleInit();
第三步:
public:
CEX2Doc* GetDocument();
_ConnectionPtr m_pConnection;//定义一个指向connection对象的指针
第四步:
void CEX2View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);//连接数据库
}
}
catch(_com_error e)//捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!"\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
第五步:
CEX2View::~CEX2View()
{
m_pConnection->Close();
m_pConnection.Release();
}
编译时候出错:
g:\c++\myprojects\ex2\ex2view.h(29) : error C2146: syntax error : missing ';' before identifier 'm_pConnection'
g:\c++\myprojects\ex2\ex2view.h(29) : error C2501: '_ConnectionPtr' : missing storage-class or type specifiers
g:\c++\myprojects\ex2\ex2view.h(29) : error C2501: 'm_pConnection' : missing storage-class or type specifiers
G:\c++\MyProjects\EX2\EX2View.cpp(48) : error C2065: 'm_pConnection' : undeclared identifier
G:\c++\MyProjects\EX2\EX2View.cpp(48) : error C2227: left of '->Close' must point to class/struct/union
G:\c++\MyProjects\EX2\EX2View.cpp(49) : error C2228: left of '.Release' must have class/struct/union type
G:\c++\MyProjects\EX2\EX2View.cpp(77) : error C2228: left of '.CreateInstance' must have class/struct/union type
G:\c++\MyProjects\EX2\EX2View.cpp(80) : error C2227: left of '->Open' must point to class/struct/union
G:\c++\MyProjects\EX2\EX2View.cpp(80) : error C2065: 'adModeUnknown' : undeclared identifier
G:\c++\MyProjects\EX2\EX2View.cpp(83) : error C2061: syntax error : identifier '_com_error'
------解决方案--------------------
把这个加上试试:
#import "c:\program files\common files\system\ado\msado15.dll" named_guids rename_namespace("ADOCG") rename("EOF", "EndOfFile") rename("BOF","FirstOfFile")
using namespace ADOCG;
#include "icrsint.h"
[解决办法]rebuild all
[解决办法]_ConnectionPtr m_pConnection;
程序不能识别你的 m_pConnection,也就是说不知道_ConnectionPtr类,所以应是缺少定义_ConnectionPtr的文件
[解决办法]同意楼上的
[解决办法]是不是那个#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")//连接数据库
应该移动到#endif的上面去?是不是这句根本就没有被编译?