1: START VC++ > FILE > NEW> MFC APPWIZARD(exe) ENTER sanjayshop(DATABASENAME) > OK DIALOG BASED > NEXT \\UNCHECK ACTIVEX AND ABOUT BOX.(WE DON'T NEED THAT) FINISH. The wizard creates a form. The new form has two buttons OK and CANCEL. Add the lines which is given in the step 1 of the table. These lines make access to the ADO Library. Close and Save it.
2: Add two functions which is given in step 2 of table. close and save it this file. Add list box onto form and declare its variable using class wizard. Change the following things while add it as member variable. VARIABLE NAME = m_lbpart. CATEGORY = Control. VARIABLE TYPE = CListBox.
3: All you have left to do is write the code that will run a Query and populate the list box. DOUBLE CLICK on button OK and that will open a dialog box ask you , what you want to name the function for its CLICK event.Left name as OnOK()the default pone. PRESS OK. Add the code which is given in the table. After the variable are declared , a connection string is assigned to bConn and a query string is assigned to vQuery. An instance of a recordset object is created , and then its OPEN method connects to the database , runs query , and stores its results. After that loops is run that moves through each row in the result set. In the loop the description column is copied to a variable and added to the list box. BY , default the list box will automatically sort the strings as they are added.
NOW YOU ARE READY TO TRY IT OUT ! SAVE AND COMPILE.
1: //File view > header file folder > StdAfx.h #include #import"c:\program files\common files\system\ado\msado15.dll"no_namespace rename ("EOF","adoEOF") 2: // Add two functions for initializing Ado. class view > CsanjayshopApp > InitInstance BOOL CsanjayshopApp::InitInstance() { AfxOleInit(); AfxEnableControlContainer(); (several more lines already exist there) } 3: After adding function for button write the following code under that. void CsanjayshopDlg::OnOK() { //CDialog::OnOK(); _RecordsetPtr pRecordSet; _variant_t vRecordSetField; Cstring sRecordSetField; _bstr_t bConn("Provider=SQLOLEDB.1" Persist Security Info=True; User ID=root; password=;Initial Catalog=sanjayshop;Data Source=local host"); _variant_t vQuery(" SELECT * from PART ;"); pRecordSet.CreateInstance(__uuidof(Recordset)); pRecordSet->Open(vQuery,bConn,adOpenDynamic,adLockOptimistic,adCmdText); while(!pRecordSet->GetadoEOF()) { vRecordSetField = pRecordSet->GetCollect(L"Description"); sRecordSetField = vRecordSetField.bstrVAL; m_lbPart.AddString(sRecordSetField); pRecordSet->MoveNext(); } }
Prof. Ashay Dharwadker