VS2013 MFC ODBC connection SQL Server database programming (ii)
Reprint Please specify: http://blog.csdn.net/my_acm/article/category/2616577
In the previous article, I explained how to connect to a SQL Server database
Refer to Http://wenku.baidu.com/link?url=h1rGgnhSmnclH2lFexHmlP_SZ2LUDRy5xM4wrbTfYlRXZLj2o0ugMT_ below Pfgrxa1xi2pm8nuueckgqra6d77ziedoymaqluudmaqqz2nqytnm
Some implementations are made, as shown below, and the full functionality implementation will be given in the next article.
First, on the basis of connecting the database, make a picture as shown in the interface, this is not difficult.
You can see that in the connection database score.h, the Cscore class inherits the CRecordset class, with six member variables corresponding to six fields in the database.
We will edit the box, the List control binds the variable separately, as shown in.
Then add the following code to the OnInitDialog function in TestDlg.cpp.
The Add member function void List_all () is used to display all of the data in the table to the list control.
The following is a concrete implementation of List_all.
void Ctestdlg::list_all (CString str) {m_list. Deleteallitems (); Cscore m_score;try{if (M_score. IsOpen ()) M_score. Close (); if (!m_score. Open (Crecordset::snapshot, str)) {MessageBox (l "Failed to open database", l "Database error", MB_OK); return;}} catch (CDBException *e) {e->reporterror ();} int nindex = 0;m_score. MoveFirst (); CString Uscore, Tscore, Fscore;while (!m_score. IsEOF ()) {Lv_item Litem;litem.mask = Lvif_text;litem.iitem = Nindex;litem.isubitem = 0;litem.psztext = L ""; m_list. InsertItem (&litem); m_list. Setitemtext (nindex, 0, M_score.m_stuid); m_list. Setitemtext (nindex, 1, m_score.m_stuname); m_list. Setitemtext (nindex, 2, M_score.m_stuclass); Uscore. Format (L "%3d", M_score.m_usualscore); Tscore. Format (L "%3d", M_score.m_testscore); Fscore. Format (L "%3d", M_score.m_totalscore); m_list. Setitemtext (nindex, 3, Uscore); m_list. Setitemtext (nindex, 4, Tscore); m_list. Setitemtext (nindex, 5, Fscore); M_score. MoveNext (); nindex++;} M_score. Close ();}
VS2013 MFC ODBC connection SQL Server database programming (ii)