Apologies if this question seems silly. Ive been researching a way to get C++ to talk to a simple access database for the last week. I started by pain stakingly searching the net for examples on how to get it to first work on a windows application.
I finally managed it to get it to connect and talk to the access database in a console application, so I moved the code to an ASP.NET web services application, however this is where Ive hit a large wall.
Ive scanned searched through the web and this forum multiple times in search of an answer but Im still lost as to why it wont work.
Anyway, here the code I just copied over basically into a wizard created C++ asp.net web service.
testingClass.cpp
----------------
#include "stdafx.h"
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")
#include "ws2Class.h"
#include "Global.asax.h"
#include <afx.h>
int main()
{ return 0; }
namespace ws2
{
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string "Hello, World!".
// To test this web service, ensure that the .asmx file in the deployment path is
// set as your Debug HTTP URL, in project properties.
// and press F5.
String __gc* ws2Class::HelloWorld()
{
CoInitialize(NULL);
_ConnectionPtr m_pConn;
m_pConn.CreateInstance (__uuidof(Connection));
m_pConn->Open( \
_bstr_t ( "Provider=Microsoft.Jet.OLEDB.4.0; \
Data Source = c:\\news_db.mdb" ), \
_bstr_t ( "Admin" ), \
_bstr_t ( "" ), \
adModeUnknown );
_CommandPtr pCommand;
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConn;
pCommand->CommandText = "Select * From news";
_RecordsetPtr pRecordset;
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic,
adLockBatchOptimistic, adCmdUnknown);
pRecordset->Fields->GetItem ("News_ID")->Properties->
GetItem ("Optimize")->Value = VARIANT_TRUE;
pRecordset->Sort = "News_ID";
pRecordset->Filter = "Topic_Posted LIKE This*";
while (!pRecordset->GetadoEOF())
{
CString str = (char *) (_bstr_t) pRecordset->Fields->
GetItem("Topic_Posted")->Value;
str = "working";
pRecordset->Fields->GetItem("Topic_Posted")->Value = (_bstr_t) str;
printf(str);
pRecordset->MoveNext();
}
pRecordset->Filter = (long) adFilterNone;
pRecordset->UpdateBatch(adAffectAll);
pRecordset->Close();
CoUninitialize();
return S"1";
}
};
-----
by far not the most elegant piece of code. Its copied and pasted bits of examples from around the web and some weird things happen (like not having the int main function creates an error. But anyway, all Im really trying to do is get a webservice talk to a database and modify it.
The program works as a windows console application, but breaks when its a web service. To be more specific, it crashes when it hits the m_pConn->Open(.....);.
If anyone has a more elegant way to connect to a database, or simply a way to fix my code, it would be appreciated.
Thanks for your time anyway
I finally managed it to get it to connect and talk to the access database in a console application, so I moved the code to an ASP.NET web services application, however this is where Ive hit a large wall.
Ive scanned searched through the web and this forum multiple times in search of an answer but Im still lost as to why it wont work.
Anyway, here the code I just copied over basically into a wizard created C++ asp.net web service.
testingClass.cpp
----------------
#include "stdafx.h"
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")
#include "ws2Class.h"
#include "Global.asax.h"
#include <afx.h>
int main()
{ return 0; }
namespace ws2
{
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string "Hello, World!".
// To test this web service, ensure that the .asmx file in the deployment path is
// set as your Debug HTTP URL, in project properties.
// and press F5.
String __gc* ws2Class::HelloWorld()
{
CoInitialize(NULL);
_ConnectionPtr m_pConn;
m_pConn.CreateInstance (__uuidof(Connection));
m_pConn->Open( \
_bstr_t ( "Provider=Microsoft.Jet.OLEDB.4.0; \
Data Source = c:\\news_db.mdb" ), \
_bstr_t ( "Admin" ), \
_bstr_t ( "" ), \
adModeUnknown );
_CommandPtr pCommand;
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConn;
pCommand->CommandText = "Select * From news";
_RecordsetPtr pRecordset;
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic,
adLockBatchOptimistic, adCmdUnknown);
pRecordset->Fields->GetItem ("News_ID")->Properties->
GetItem ("Optimize")->Value = VARIANT_TRUE;
pRecordset->Sort = "News_ID";
pRecordset->Filter = "Topic_Posted LIKE This*";
while (!pRecordset->GetadoEOF())
{
CString str = (char *) (_bstr_t) pRecordset->Fields->
GetItem("Topic_Posted")->Value;
str = "working";
pRecordset->Fields->GetItem("Topic_Posted")->Value = (_bstr_t) str;
printf(str);
pRecordset->MoveNext();
}
pRecordset->Filter = (long) adFilterNone;
pRecordset->UpdateBatch(adAffectAll);
pRecordset->Close();
CoUninitialize();
return S"1";
}
};
-----
by far not the most elegant piece of code. Its copied and pasted bits of examples from around the web and some weird things happen (like not having the int main function creates an error. But anyway, all Im really trying to do is get a webservice talk to a database and modify it.
The program works as a windows console application, but breaks when its a web service. To be more specific, it crashes when it hits the m_pConn->Open(.....);.
If anyone has a more elegant way to connect to a database, or simply a way to fix my code, it would be appreciated.
Thanks for your time anyway