When programming, we need to save various types of data received in real time and replay the data in the future. We should create a data file, which can be read and written by common files, however, when there are many data types and data playback is required at any time, a large dynamic array is required during programming. This will occupy a large amount of system resources and even cause program crashes; the database can better solve this problem. We put the data into the data source file and access it through programming interfaces. ODBC provides unified programming interfaces for various types of Database management systems. In the following articles, first, explain the application method of ODBC technology through an example, and then explain how to save the serial port data in the data source file in real time, this section introduces the application of ODBC technology in VC (the previous example does not involve serial port technology, so that users only want to understand ODBC technology, and provides examples for saving serial port programming data using ODBC technology, although the previous routines are irrelevant to the serial communication, if you are not familiar with the database, please read them with patience.
1. Open the ODBC data source in the control panel, click the user DSN option, and click Add in the displayed interface, as shown in:
Then, click the "finish" button, and enter the data source name (you can name it at will and set it to biao)
Click OK.
2. Create a database in ACCESS. In this example, It is data (only including the number, name, and score columns). You can COPY the data from the example provided by me.
3. Create an application project
(1) Open the New option of the File menu and select Projects,
Select MFC AppWizard (exe) and enter the job name. In this example, DATA1 is used.
(2) Upload the database file data to the new project directory.
(3) set the application type to SDI. In the Step 2 dialog box, select the Header Files Only option. In Step 6, set the view base class to CScrollView.
(4) use ClassWizard to create a record set class. Select New from the Add Class Menu and follow the fill dialog box: (Note: A CRecordset object represents a record set queried from the data source .)
(5) Click OK to enter the Data Source, select the biao Data Source, and select the Dynast option, as shown in:
After selecting the data source, ClassWizard prompts you to select a table. As shown in:
(6) In ClassWizard, click the Member Variables label for the newly generated CDataSet class. In this case, ClassWizard should generate the following data members according to the name of the database column:
(7) add data members to the CData1View class
(8) Add the following members to Cdata1Doc:
(9) edit the OnDraw () and OnInitialUpdate () functions in DATA1View. cpp:
Void CDATA1View: OnDraw (CDC * pDC)
{
CDATA1Doc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
// TODO: add draw code for native data here
TEXTMETRIC tm;
PDC-> GetTextMetrics (& tm );
Int nLineHeight = tm. tmHeight + tm. tmExternalLeading;
CPoint pText (0, 0 );
Int y = 0;
CString str;
If (m_pSet-> IsBOF () {// detects empty recordset
Return;
}
M_pset-> movefirst (); // fails if recordset is empty
While (! M_pset-> iseof ()){
PDC-> textout (ptext. X, ptext. Y, m_pset-> m_number );
PDC-> textout (ptext. x + 1000, ptext. Y, m_pset-> m_name );
PDC-> textout (ptext. x + 4000, ptext. Y, m_pset-> m_score );
M_pset-> movenext ();
Ptext. Y-= nlineheight;
}
}
Void cdata1view: oninitialupdate ()
{
Cscrollview: oninitialupdate ();
Csize sizetotal (8000,105 00 );
Setscrollsizes (mm_hienglish, sizetotal );
M_pset = & getdocument ()-> m_dataset;
If (m_pset-> isopen ()){
M_pset-> close ();
}
M_pset-> open ();
}
(10) add # include "dataset. H" to data1view. cpp"
(11) The compilation result should be as follows: