描述:
我用ATL编写访问数据库组件(用ADO)是为什么编译不了啊
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF" , "EndOfFile")
#include "stdafx.h"
#include "ATLTEST.h"
#include "Math.h"
#include <windows.h>
#include <ole2.h>
#include <iostream.h>
#include <tchar.h>
/////////////////////////////////////////////////////////////////////////////
// CMath
STDMETHODIMP CMath::print()
{
_RecordsetPtr pRecord = NULL;
_ConnectionPtr pConnection = NULL;
_bstr_t strcon("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb");
try
{
pConnection.CreateInstance(__uuidof(Connection));
pConnection->Open(strcon,"","",adConnectUnspecified);
pRecord.CreateInstance(__uuidof(Recordset));
LPCSTR strSQL = _TEXT("select * from table1");
pRecord->Open(_bstr_t(strSQL),
_variant_t((IDispatch *)pConnection, true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
while( !pRecord->EndOfFile )
{
_variant_t thevariant1;
_variant_t thevariant2;
VariantInit(&thevariant1);
VariantInit(&thevariant2);
thevariant1 = pRecord->GetCollect("编号");
thevariant2 = pRecord->GetCollect("姓名");
if( thevariant1.vt != VT_NULL && thevariant2.vt != VT_NULL)
{
cout<<(char *)_bstr_t(thevariant1);
cout<<" "<<(char *)_bstr_t(thevariant2)<<endl;
}
pRecord->MoveNext();
}
}
catch( _com_error e)
{
::MessageBoxA(NULL,e.ErrorMessage(),"错误",MB_OK);
pRecord->Close();
pConnection->Close();
pRecord = NULL;
pConnection = NULL;
return S_FALSE;
}
pRecord->Close();
pConnection->Close();
pRecord = NULL;
pConnection = NULL;
return S_OK;
}
编译错误
reating Type Library...
Microsoft (R) MIDL Compiler Version 5.01.0164
Copyright (c) Microsoft Corp 1991-1997. All rights reserved.
Processing D:\vc\ATLTEST\ATLTEST.idl
ATLTEST.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\oaidl.idl
oaidl.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\objidl.idl
objidl.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\unknwn.idl
unknwn.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\wtypes.idl
wtypes.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\basetsd.h
basetsd.h
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\guiddef.h
guiddef.h
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\ocidl.idl
ocidl.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\oleidl.idl
oleidl.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\servprov.idl
servprov.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\urlmon.idl
urlmon.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\msxml.idl
msxml.idl
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\oaidl.acf
oaidl.acf
Processing C:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\ocidl.acf
ocidl.acf
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
ATLTEST.cpp
Math.cpp
D:\vc\ATLTEST\Math.cpp(18) : error C2065: '_RecordsetPtr' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(18) : error C2146: syntax error : missing ';' before identifier 'pRecord'
D:\vc\ATLTEST\Math.cpp(18) : error C2065: 'pRecord' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(19) : error C2065: '_ConnectionPtr' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(19) : error C2146: syntax error : missing ';' before identifier 'pConnection'
D:\vc\ATLTEST\Math.cpp(19) : error C2065: 'pConnection' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(21) : error C2065: '_bstr_t' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(21) : error C2146: syntax error : missing ';' before identifier 'strcon'
D:\vc\ATLTEST\Math.cpp(21) : error C2065: 'strcon' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(26) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
D:\vc\ATLTEST\Math.cpp(27) : error C2228: left of '.CreateInstance' must have class/struct/union type
D:\vc\ATLTEST\Math.cpp(27) : error C2065: 'Connection' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(28) : error C2227: left of '->Open' must point to class/struct/union
D:\vc\ATLTEST\Math.cpp(28) : error C2065: 'adConnectUnspecified' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(30) : error C2228: left of '.CreateInstance' must have class/struct/union type
D:\vc\ATLTEST\Math.cpp(30) : error C2065: 'Recordset' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(35) : error C2227: left of '->Open' must point to class/struct/union
D:\vc\ATLTEST\Math.cpp(36) : error C2065: '_variant_t' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(37) : error C2065: 'adOpenDynamic' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(38) : error C2065: 'adLockOptimistic' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(39) : error C2065: 'adCmdText' : undeclared identifier
D:\vc\ATLTEST\Math.cpp(42) : error C2227: left of '->EndOfFile' must point to class/struct/union
D:\vc\ATLTEST\Math.cpp(42) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Generating Code...
Error executing cl.exe.
ATLTEST.dll - 22 error(s), 1 warning(s)