// The following is the main code void createdbdlg :: OnRead () {// Todo: Add Your Control Notification Handler Code Here
CDATABASE DATABASE; CSTRING SQLSTRING; CSTRING SCATID, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid, Scatid PATH Here Int IREC = 0; // Create ODBC Connection Dinamical SDSN.Format ("ODBC; Driver = {% S}; DSN = '; DBQ =% S", SDRIVER, SFILE); Try {// Open the Database Database.open (NULL, FALSE, FALSE, SDSN); // Allocate The RecordSet CRecordset Recset (& Database);
// build the sql statement sqlstring = "select catid, category" "from categories";
// Execute the query recset.Open (CRecordset :: forwardOnly, SqlString, CRecordset :: readOnly); ResetListControl (); // Column width and heading m_ListControl.InsertColumn (0, "Cat ID", LVCFMT_LEFT, -1,0) M_ListControl.insertColumn (1, "Category", LVCFMT_LEFT, -1, 1); M_ListControl.SetColumnWidth (0, 70); M_ListControl.SetColumnWidth (1, 200);
// Loop through Each Record While (! Recset.iseof ()) {// Copy Each Column Into a variable recset.getfieldValue ("catid", scatid); recset.getfieldValue ("category", scategory;
// INSERT VALUES INTO The List Control IREC = M_ListControl.InsertItem (0, Scatid, 0); M_ListControl.SetItemtext (0, 1, Scategory);
// goto next record recset.MoveNext ();} // Set column width //m_ListControl.SetColumnWidth(0, LVSCW_AUTOSIZE); //m_ListControl.SetColumnWidth(1, LVSCW_AUTOSIZE); // Close the database database.Close (); } CATCH (CDBException, e) {// If a database exception occured, show error msg AfxMessageBox ( "Database error:" e-> m_strError);} END_CATCH;} void CReadDBDlg :: ResetListControl () {m_ListControl.DeleteAllItems () ; int iNbrOfColumns; CHeaderCtrl * pHeader = (CHeaderCtrl *) m_ListControl.GetDlgItem (0); if (pHeader) {iNbrOfColumns = pHeader-> GetItemCount ();} for (int i = iNbrOfColumns; i> = 0; i--) {M_listcontrol.deletecolumn (i);}}