I am maintaining a legacy codebase and need to write a small console application in Visual C++ 6 that accesses a SQL Server database via ADODB. I am an experienced C/C++ developer, but haven't spent much time with MS C++, which as far as I can tell is totally different than g++ and chock full of nonstandard language extensions.
Whenever I try to follow a tutorial on the web, I get a zillion errors. It doesn't recognize a whole bunch of identifiers (e.g. CComPtr, HRESULT, _RecordsetPtr, etc.) and I'm pretty sure the problem is I'm somehow setting up my project wrong. I have tried basically every console app AppWizard available, and none will make the demo code I found work.
Can someone explain to me how I set up a Visual C++ 6 application that can, for example, do a count(*) from a DB table? I am thinking my problem has something to do with non properly including various ATL or whatever libraries that ADODB depends on... I've figured out h开发者_开发百科ow to #import msado15.dll but still no dice.
Please help!!! Thanks in advance,
Jason
UPDATE: have now gotten it to compile, but am getting a "Debug Assertion Failed" when I try to open my ADO connection. It comes from atlbase.h line 474 and the assertion is "p != 0".
UPDATE 2: Here's my code
#import "C:\Program Files\Common Files\System\ADO\msdo15.tlb" no_namespace rename("EOF","A_EOF")
#include "stdafx.h"
#include <objbase.h>
#include <initguid.h>
#include <comdef.h>
#include <atlbase.h>
#include <adoid.h>
#include <adoint.h>
int main(int argc, char* argv[])
{
HRESULT hr;
CComPtr<ADORecordset> m_pSet;
CComPtr<ADOConnection> m_pConn;
char ret[128];
CComBSTR connstr = (CComBSTR) "driver=SQL Server;server=SQL1;uid=ffffddddd;pwd=aaaasss;database=MyDB";
CoCreateInstance(CLSID_CADOConnection, NULL, CLSCTX_INPROC_SERVER, IID_IADOConnection, (LPVOID *) &m_pConn);
CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_INPROC_SERVER, IID_IADORecordset, (LPVOID *) &m_pSet);
printf("Here %d!\n", (int) &m_pConn);
m_pConn->Open(connstr, (CComBSTR) "", (CComBSTR) "", adOpenUnspecified);
//m_pConn->ConnectionString = connstr;
//m_pConn->Open("","","",NULL);
printf("Here!\n");
m_pSet->Open(CComVariant((CComBSTR) "SELECT COUNT(*) AS Cnt FROM VARIANCESWAP_INDIC"), CComVariant(m_pConn), adOpenKeyset, adLockOptimistic, adCmdText);
CComPtr<ADOFields> pFields = NULL;
m_pSet->get_Fields(&pFields);
CComPtr<ADOField> cnt = NULL;
pFields->get_Item(CComVariant(0), &cnt);
CComVariant dbValue;
cnt->get_Value(&dbValue);
sprintf(ret, "%S", dbValue.bstrVal);
if(m_pSet != NULL) m_pSet->Close();
if(m_pConn != NULL) m_pConn->Close();
printf("Hello World!\n");
return 0;
}
If you're not getting a definition for HRESULT then you probably have not #included <windows.h>.
Google is your friend. Search for missing types and generally some kind soul, or MSDN, will tell you what file you need to include in your program to get it working.
Make sure you check the HRESULT return values from your function calls, specifically the CoCreateInstance
calls here. I suspect they are failing because it looks like you haven't called CoInitialize
, but check what error code you get.
You haven't initialized COM.
Also, you are not checking the return values of COM calls like CoCreateInstance or Open. You are supposed to check the result of each COM call.
Suggested reading
- Using ADO with Microsoft Visual C++
- KB183606 ActiveX Data Objects (ADO) Frequently Asked Questions
精彩评论