Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help Pls - C++ and ADO problem

Help Pls - C++ and ADO problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasec++debugging
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonaras
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • A antonaras

      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

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      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

      A J 2 Replies Last reply
      0
      • S 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

        A Offline
        A Offline
        antonaras
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • A antonaras

          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

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • S Steve S

            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

            A Offline
            A Offline
            antonaras
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S 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

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              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.

              S 1 Reply Last reply
              0
              • J Jorgen Sigvardsson

                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.

                S Offline
                S Offline
                Steve S
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups