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. linking a microsoft access database to a vc++ program

linking a microsoft access database to a vc++ program

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasetutorialquestion
3 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
    Anonymous
    wrote on last edited by
    #1

    hai, i am trying to lern vc++, could u please tell me how to connect a database from outside to the vc++ program for getting the datas from the database ? thank you, yours sincerely, ann

    D V 2 Replies Last reply
    0
    • A Anonymous

      hai, i am trying to lern vc++, could u please tell me how to connect a database from outside to the vc++ program for getting the datas from the database ? thank you, yours sincerely, ann

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      If you are using MFC, check out the CRecordset and CDatabase classes.


      "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

      1 Reply Last reply
      0
      • A Anonymous

        hai, i am trying to lern vc++, could u please tell me how to connect a database from outside to the vc++ program for getting the datas from the database ? thank you, yours sincerely, ann

        V Offline
        V Offline
        Vikram Kashyap
        wrote on last edited by
        #3

        Hi, If you are using MFC, then u can use the following code to connect to access database. BOOL CMyDatabaseTesting::ConnectToDatabase() { if(!m_pdb) m_pdb = new CDatabase; CString StrConnectString; try { //Two Methods of connecting //1. Through the database path as described below //2. Using DSN, i.e, "DSN=szDSN;UID=szUsername;PWD=szPassword; StrConnectString.Format("Driver={Microsoft Access Driver (*.mdb)};Dbq=%s;UID=%s;PWD=%s",strDBPath/*Contains the database path*/, "Admin",szPassword/*Password of database (fi any)*/); m_pdb->OpenEx(StrConnectString,CDatabase::noOdbcDialog); } catch(CDBException *e) { if(e->m_strError.Find("Login failed") != -1) { MessageBox(0, e->m_strError, "Error1", MB_OK); return FALSE; } else if((e->m_strError.Find("Server user id") != -1) && (e->m_strError.Find("is not a valid user in database") != -1)) { MessageBox(0, e->m_strError, "Error2", MB_OK); return FALSE; } else if(e->m_strError.Find("Data source name not found and no default driver specified") != -1) { MessageBox(0, e->m_strError, "Error3", MB_OK); e->Delete(); if(m_pdb) { if(m_pdb->IsOpen()) m_pdb->Close(); delete m_pdb; m_pdb = NULL; } return FALSE; } else { //Database related error MessageBox(0, e->m_strError, "Database Error", MB_OK); return FALSE; } if(m_pdb) { if(m_pdb->IsOpen()) m_pdb->Close(); delete m_pdb; m_pdb = NULL; } e->Delete(); return FALSE; } return TRUE; } Where m_pdb is a pointer to CDatabase. Also don't forget to include in stdafx.h file. That's it, you are connect to the database. Enjoy... :wtf: Vikram Kashyap "You will never fail until you stop trying"

        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