Help Pls - C++ and ADO problem
-
Hi again I'm trying to connect to an access db using ADO i use some sample codes to perform a simple SELECT query and display them through a recordset. I've tryed really hard to make it work i've searched everywhere pls help me find the problem. This is the code i use::
#include #include #include #import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") void main(int argc, char* argv[]) { HRESULT hr = S_OK; _ConnectionPtr m_pConn; try { HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =ADOTestDB.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; pCommand->CommandText = "Select Name,Dept From Student"; _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Name")->Value; valField2 = pRecordset->Fields->GetItem("Dept")->Value.intVal; printf("%d - %s\n",valField2,(LPCSTR)valField1); pRecordset->MoveNext(); } } }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } m_pConn->Close(); }
i get an exeption "Can't create an intance of ADO.Connection" and the i asks if i want to debug Pls help me i'm trying a lot to connect to a db and enything i do seems to be wrong and the truth is that everytime i post a msg about C++ and ADO no one replies. pls i'm desparate -
Hi again I'm trying to connect to an access db using ADO i use some sample codes to perform a simple SELECT query and display them through a recordset. I've tryed really hard to make it work i've searched everywhere pls help me find the problem. This is the code i use::
#include #include #include #import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") void main(int argc, char* argv[]) { HRESULT hr = S_OK; _ConnectionPtr m_pConn; try { HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =ADOTestDB.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; pCommand->CommandText = "Select Name,Dept From Student"; _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Name")->Value; valField2 = pRecordset->Fields->GetItem("Dept")->Value.intVal; printf("%d - %s\n",valField2,(LPCSTR)valField1); pRecordset->MoveNext(); } } }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } m_pConn->Close(); }
i get an exeption "Can't create an intance of ADO.Connection" and the i asks if i want to debug Pls help me i'm trying a lot to connect to a db and enything i do seems to be wrong and the truth is that everytime i post a msg about C++ and ADO no one replies. pls i'm desparateADO is a set of COM objects. To use COM from any thread, you need to call CoInitialize (or for advanced users that won't run on W9x or ME, CoInitializeEx). Try putting CoInitialize(NULL); after your declaration of hr, move your connection pointer inside your try block [it's a smart pointer, and needs to go out of scope or be released before calling CoUninitialize()] Add CoUninitialize(); after your catch block. Steve S Developer for hire
-
ADO is a set of COM objects. To use COM from any thread, you need to call CoInitialize (or for advanced users that won't run on W9x or ME, CoInitializeEx). Try putting CoInitialize(NULL); after your declaration of hr, move your connection pointer inside your try block [it's a smart pointer, and needs to go out of scope or be released before calling CoUninitialize()] Add CoUninitialize(); after your catch block. Steve S Developer for hire
Hey Thanks Steve is FINALLY WORKING!!! thanks you Steve S i'm so happy i was about to give up BTW do you know any good reference that i can use to learn about ADO and COM objects because using sample codes i cannot understand the reall structure of ADO i just copy paste without understanding. Thanks again Steve S i appreciate all the help
-
Hey Thanks Steve is FINALLY WORKING!!! thanks you Steve S i'm so happy i was about to give up BTW do you know any good reference that i can use to learn about ADO and COM objects because using sample codes i cannot understand the reall structure of ADO i just copy paste without understanding. Thanks again Steve S i appreciate all the help
No problem; I'm more of an OLEDB man myself, but SAMS had a "Database Programming in VC++" which I think covered most techniques. COM itself has a relatively small surface area to learn, but there are lots of things that you need to know. Best bet is to look for tutorials in places like CodeProject. Steve S Developer for hire
-
No problem; I'm more of an OLEDB man myself, but SAMS had a "Database Programming in VC++" which I think covered most techniques. COM itself has a relatively small surface area to learn, but there are lots of things that you need to know. Best bet is to look for tutorials in places like CodeProject. Steve S Developer for hire
Thanks again i found that book of Sams and Db programming in 21 days the problem is that he use the MFC and wizards which i don't really understand either. Maybe your right the best place to learn from is the web BTW can i ask something else. i need to execute a query like: "Select Name,Dept From Student WHERE Name=? " i want the query to get the name from a variable of type string. "Select Name,Dept From Student WHERE Name=token//token is a variable of type string which get the value from a different function how can i do that? di i need to use stored procedures? Thanks again steve S
-
ADO is a set of COM objects. To use COM from any thread, you need to call CoInitialize (or for advanced users that won't run on W9x or ME, CoInitializeEx). Try putting CoInitialize(NULL); after your declaration of hr, move your connection pointer inside your try block [it's a smart pointer, and needs to go out of scope or be released before calling CoUninitialize()] Add CoUninitialize(); after your catch block. Steve S Developer for hire
I'd suggest putting CoInitialize(NULL) in WinMain/InitInstance/whatever, and CoUninitialize() in respective counterparts, if the design allows it. CoInitialize(NULL) is time consuming (although not as bad on newer operating systems, as it was back in the 9x days), and is therefore best kept out of the loop so to speak.
-- 100% natural. No superstitious additives.
-
I'd suggest putting CoInitialize(NULL) in WinMain/InitInstance/whatever, and CoUninitialize() in respective counterparts, if the design allows it. CoInitialize(NULL) is time consuming (although not as bad on newer operating systems, as it was back in the 9x days), and is therefore best kept out of the loop so to speak.
-- 100% natural. No superstitious additives.
I thought that's what I'd said, but did not explicitly make the point, so thanks for pointing it out. Good practice is to keep as much as possible out of loops - just look at the different code produced with and without optimisations turned on :) Steve S Developer for hire