Pls Help - Visual C++ with ADO
-
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 desparateThis might be helpful: http://www.codeproject.com/database/WittmerADO.asp[^] -- modified at 17:30 Tuesday 30th May, 2006