A perfect ODBC database program

Source: Internet
Author: User
Tags odbc

We know that VC development of the database program, there are generally two choices: ODBC or ADO. ODBC appears earlier and uses more people. ADO is the technology that Microsoft is vigorously supporting and developing, the programmer who is devoted to learning VC should learn this technology now.

In this case, I used the more familiar ODBC. The implementation of the program has two key places: one, the use of ODBC two classes. Second, the use of ListControl control. The view of the program uses the CRecordView class, the concrete configuration may refer to the source program. The implementation of the program is mainly done in the view file, the realization of the functions are: to increase the record, delete records, modify records, sort records, search records, and in the control table intuitive display. This program uses the Aecess database, of course, Oracle and other databases can also be used. The interface of the whole program is concise and intuitive (see the figure below).

The following are the steps:

First, according to the contents of the database to determine the style of the table, here are the following design:

1. Add Contrl variable M_listctrl to the list control controls in the view header file. Add the following statement to the OnInitialUpdate () function of the implementation file:

//设表格表题和列的宽度
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_ListCtrl.InsertColumn(0,_T("学号"),LVCFMT_IMAGE|LVCFMT_LEFT);
  m_ListCtrl.InsertColumn(1,_T("姓名"));
  m_ListCtrl.InsertColumn(2,_T("数学"));
  m_ListCtrl.InsertColumn(3,_T("英语"));
int j;
for(j=0;j<4;j++)
  {
   m_ListCtrl.SetColumnWidth(j ,90);
  }
So the list problem is set, and the width of the column is set. It is also important to note that you want to set the Styles property in the list control's property page. Because when the program runs, all the records are required to be displayed, so we add a show () function, which I encapsulate for its reusability. You can see the benefits of it in the following program. The show () function is as follows:int CLhwyView::Show()
{
  int i=0;
  m_pSet->MoveFirst();
  do
   { 
    CString s;
    s.Format("%d",m_pSet->m_column1);
    m_ListCtrl.InsertItem(i,s,0);
     m_ListCtrl.SetItemText(i,1,m_pSet->m_column2);
    s.Format("%d",m_pSet->m_column3);
     m_ListCtrl.SetItemText(i,2,s);
    s.Format("%d",m_pSet->m_column4);
    m_ListCtrl.SetItemText(i,3,s);
    i++;
    m_pSet->MoveNext();
    
    } while(!m_pSet->IsEOF());
    m_pSet->MoveFirst();
    return i;
}

So the whole process of initialization of the work is basically completed. Because the domain in the database is in Chinese characters, the recordset appears

Strange m_column variable. If the use of English, there will be no such an intuitive phenomenon. Here M_column1 the number in the corresponding database, m_column2 the name in the corresponding database, and so on. In the database, I set the data type of the first Name field to text, and the rest to numbers. The list control controls only display text. So before displaying variables that are not text, they are formatted with the format () function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.