C++ importing and using ADO
I have two short questions involving importing and using ADO in a C++ project. I am not experienced with ADO in any form.
First, for the time being the database aspect of my program need only run on Windows. Is it enough to test if _WIN32 or _WIN64 is defined before running ADO specific code, or are there better approaches? I am using Visual C++ 2010 Express as my compiler.
Second, I am following this page as my guide. I've included the #import statement for msado15.dll. The #import directive is underlined red, and the error I receive when hovering over it says "...cannot open source file path/to/msado15.tlh," and any ADO stuff I copy into the source stays red underlined. I've checked the directory listed in the error message and msado15.tlh is there, if that matters. Also, the program compiles (it crashes though after executing, but that's something else I'll diagnose separately).
I'm pretty clueless about why this is happening. Any help o开发者_如何学运维r advice would be appreciated.
The way we do it, we add the following to out VC++ Directories / Include files
$(ProgramFiles)\Common Files\System\ado
And then we import like this
in the header:
#import "msado15.dll" rename_namespace("ADO") rename("EOF", "EndOfFile") no_implementation
At the top of the cpp file
#import "msado15.dll" rename_namespace("ADO") rename("EOF", "EndOfFile") implementation_only
A little different, since we prefer to keep the namespace, and use a different EOF rename.
C++ Import help on MSDN
C++ Import Attributes on MSDN
I had the same problem when I started with ADO. (red underlined, can't find...) It drove me nuts but after simply compiling, the underline went away and everything worked fine.
I use
#import "C:\Program\Delade filer\System\ado\msado15.dll" rename_namespace("USEADO"),rename("EOF","EndOfFile")
in my header.
In cpp eg:
#include "stdafx.h"
int SQLsetInfo(THING *Thing, GADGET *Gadget)
{
HRESULT hr;
USEADO::_ConnectionPtr connection;
USEADO::_RecordsetPtr recordset;
//Initialize COM
if(FAILED(hr = CoInitialize(NULL)))
{ MessageBox( NULL, L"Initialize COM Failed", L"MyProg!",MB_ICONEXCLAMATION |MB_OK);
//Do something, eg shut down DB stuff and continue without or exit program
//Insert error handeler below
return hr;//TODO Ad error handeling see line 149
}
if(FAILED(hr = connection.CreateInstance(__uuidof(USEADO::Connection))))
{ MessageBox( NULL, L"Create connection instance Failed", L"MyProg!",MB_ICONEXCLAMATION |MB_OK);
//Do something, eg shut down DB stuff and continue without or exit program
return hr;
}
if(FAILED(hr = recordset.CreateInstance(__uuidof(USEADO::Recordset))))
{ MessageBox( NULL, L"Create recordset instance Failed", L"MyProg!",MB_ICONEXCLAMATION |MB_OK);
//Do something, eg shut down DB stuff and continue without or exit program
return hr;
}
connection->CursorLocation = USEADO::adUseServer; //http://dev.mysql.com/tech-resources/articles/vb-cursors-and-locks.html
//Try to connect to SQL server
try { connection->Open(L"YOUR CONNECTION STRING", USEADO::adConnectUnspecified); }
catch(...) {std::cout << "!!! connection->Open(ConnectionString FAILED !!!" << std::endl; }
I'm not an expert in C++, but this works fine for me. Hope it helps you to. If anyone else here has suggestions/critic to the above snippet I'm looking forward to it....
精彩评论