Simple operations on database files in MFC (ADD, modify, search, and delete)

Source: Internet
Author: User
Tags mdb database textout
Requirement: Create a new database file (Microsoft access), which includes the student's information student ID, Name, and English score. Create an MFC project in VisualC6.0, create a student recordset and bind it to a database file. Add information in the file in C

Requirement: Create a new database file (Microsoft access), which includes the student's information student ID, Name, and English score ), create an MFC project in Visual C 6.0, create a StudentRecordSet, and bind it to a database file. Add information in the file in C

Requirement: Create a new database file (Microsoft access), which includes the student's information student ID, Name, and English score ), create an MFC project in Visual C ++ 6.0, create a StudentRecordSet, and bind it to a database file. Add, search, output, and delete information in the file in C ++.

1. First create an MFC project named data

2. Create a new Microsoft access File named "source" in the project file.

3. Open the source. mdb file, click the designer to create a table, and enter ID Name English in sequence for the field names in the table. The first two data types are text, and the last data type is numbers. Right-click the row of the ID and set it as the primary key (the unique one is set as the primary key. For example, each student in a class has a student ID, which is different and unique ). When table 1 is disabled, the system prompts whether to save the modification to Table 1. Click "yes" and save it as the table name "source.

4. Set the data file as a data source. The specific method is to open the control panel of the computer, find the management tool in it, find the data source, double-click to open, click Add, select Driver do Microsoft Access (*. mdb), in the pop-up dialog box, select the data source name, and select the database file (source. mdb ).

5. Next, perform operations on the MFC project. Right-click data classes in class View, new Class (add class), Type MFC class. Name: StudentRecordSet Base Class: CRecordset OK. In the dialog box that appears, select source for ODBC and Dynaset for RecordsetType, and OK for the StudentRecordSet class to bind to the database file.

① Click the left mouse button to save data to the source. mdb database file.

1. Right-click CDataView and select OnLButtonDown to enter the void CDataView: OnLButtonDown (UINT nFlags, CPoint point). Write the following code in it:

StudentRecordSet rs; // creates an object.

Rs. Open (); // Open the database file

Rs. AddNew (); // Add new data

Rs. m_ID = "001"; // ID value of the first record

Rs. m_Name = "Tom ";

Rs. m_English = 100;

Rs. Update (); // Update

Rs. Close (); // Close the file

An error occurred while compiling and running. 1. class StudentRecordSet: public CRecordset does not recognize the base class CRecordset. Double-click the StudentRecordSet class and add the header file # include. 2. StudentRecordSet rs is used in the View class; // creates an object. StudentRecordSet is not recognized. Double-click CDataView and add the header file # include "StudentRecordSet. h". The compilation is successful.

2. run, click the left mouse button, the interface does not respond, actually rs. m_ID = "001"; rs. m_Name = "Tom"; rs. m_English = 100; the data has been stored in the file source. mdb is in. open the file and check that it has been added.

If you continue to click the left mouse button in the running window, an error occurs. It is because the same record is added to the file, but the ID has been set as the primary key. It is unique and cannot be added again.

3. if you add a new record to the file, you need to modify the value of the following code:

StudentRecordSet rs; // creates an object.

Rs. Open (); // Open the database file

Rs. AddNew (); // Add new data

Rs. m_ID = "001"; // modify the ID value of the first record 001

Rs. m_Name = "Tom"; Modify Tom

Rs. m_English = 100; Modify 100

Rs. Update (); // Update

Rs. Close (); // Close the file

Here I change 001 to 002 Tom to Jerry 100 to 99, compile and run, left click, OK, and go to the database file source. mdb. The new record has been added. PS: Add a new record. You can also add the record directly in the file without using C ++.

② Right-click the database and display the information in the database.

1. Right-click the CDataView class and select OnRButtonDown to enter void CDataView: OnRButtonDown (UINT nFlags, CPoint point). Add the following code:

StudentRecordSet rs; // creates an object.

Rs. Open ();

Int y = 20; // controls the variation of y coordinates, line feed

CString strTemp; // Temporary Variable

CClientDC dc (this); // customer Zone

While (! Rs. IsEOF () // determines whether the end of the file is reached.

{

StrTemp. Format ("ID: % s, Name: % s, English: % d", rs. m_ID, rs. m_Name, rs. m_English); // Format

Dc. TextOut (20, y, strTemp); // The dc calls the function textout to display the content.

Rs. MoveNext (); // the pointer is in the first record by default. Use this sentence to move the pointer down.

Y + = 20; // line feed

}

Rs. Close ();

OK, compile, run, right-click, and all the content in the database is displayed on the panel. (Do not click the left mouse button. Otherwise, the same error mentioned above will appear. you can comment out the code in the left mouse button first)

③ Modify the content and the specified content

1. modify the content of the first record. Write the following code in the function of the left mouse button:

Rs. Close (); // Close the file */

StudentRecordSet rs;

Rs. Open ();

Rs. Edit ();

Rs. m_English = 60;

Rs. Update ();

Rs. Close ();

Compile and run. Click the left button and then right-click. The English score of the first record has been changed to 60.

2. comment out the added code by specifying the content modification and add the following code:

StudentRecordSet rs;

Rs. m_strFilter = "ID = '002'"; // The record whose ID is the primary key, unique, and Information Filtering. The record whose ID is 002 is modified.

Rs. Open ();

Rs. Edit ();

Rs. m_English = 80; // modify the English score

Rs. Update ();

Rs. Close ();

Compile, run, left click, right click, and the English score with ID 002 has been changed to 80

④ Content Search

In the left-click function, comment out the modified Code and write the following code:

StudentRecordSet rs;

Rs. m_strFilter = "English = 80"; // All records with an English score of 80

Rs. Open ();

CClientDC dc (this );

CString strTemp;

Int y = 20;

While (! Rs. IsEOF ())

{

StrTemp. Format ("ID: % s, Name: % s, English: % d", rs. m_ID, rs. m_Name, rs. m_English );

Dc. TextOut (20, y, strTemp); // added the display function and left click

Rs. MoveNext ();

Y + = 20; // line feed

}

Rs. Close ();

OK, compile, run, and click the left mouse button. The 80 English score is displayed. Because there are only two records in the database file and the English score of one record is 80, only one record is displayed here. PS (the rs. Update () function cannot be added for searching. This function is only followed by adding record and modifying record );

⑤ Content Deletion

Here, the deletion of the specified content is not mentioned. It is similar to the modification of the specified content. replace rs. Edit (); with rs. Delete ();

Delete all the content below.

Method 1:

1. comment out the code just written in the left-click function and add the following code:

StudentRecordSet rs;

Rs. Open ();

While (! Rs. IsEOF ())

{

Rs. Delete (); // Delete a record

Rs. MoveNext ();

}

Rs. Close ();

OK, compile and run. Left click. View database files. access

As follows:

All data is deleted. (This method is less efficient, and programming is used to delete file data. The second method below is to allow the database file source. mdb to delete data, which is highly efficient)

Method 2:

1. comment out the code of method 1.

Add the following code:

CDatabase db;

StudentRecordSet rs (& db );

CString strSql = "delete from source ";

Rs. Open ();

Db. ExecuteSQL (strSql );

Rs. Close ();

Compile, run, and left-click. OK achieves the same effect as method 1, and all data in the file is deleted.

Code for attaching the left-click function:

Void CDataView: OnLButtonDown (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default/* StudentRecordSet rs; // create an object rs. open (); // Open the database file rs. addNew (); // Add new data rs. m_ID = "002"; // The ID value rs of the first record. m_Name = "Jerry"; rs. m_English = 99; rs. update (); // Update rs. close (); // Close the file * // * StudentRecordSet rs; rs. open (); rs. edit (); rs. m_English = 60; rs. update (); rs. close (); * // modify the content/* StudentRecordSet rs; rs. M_strFilter = "ID = '002'"; rs. open (); rs. edit (); rs. m_English = 80; rs. update (); rs. close (); * // specify the content modification/* StudentRecordSet rs; rs. m_strFilter = "English = 80"; rs. open (); CClientDC dc (this); CString strTemp; int y = 20; while (! Rs. isEOF () {strTemp. format ("ID: % s, Name: % s, English: % d", rs. m_ID, rs. m_Name, rs. m_English); dc. textOut (20, y, strTemp); rs. moveNext (); y + = 20;} rs. close (); * // find and display/* StudentRecordSet rs; rs. open (); while (! Rs. isEOF () {rs. delete (); rs. moveNext ();} rs. close (); * // delete method 1 CDatabase db; StudentRecordSet rs (& db); CString strSql = "delete from source"; rs. open (); db. executeSQL (strSql); rs. close (); // Delete method 2 CView: OnLButtonDown (nFlags, point );}

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.