linking a microsoft access database to a vc++ program
-
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
If you are using MFC, check out the
CRecordset
andCDatabase
classes.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
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
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"