Access Database Access Implementation in VC/MFC

Source: Internet
Author: User

From: http://blog.csdn.net/mrdone/article/details/4868741

The other day, I got a little thing to access the ACCESS database, and it took some effort to get it done.

Although it is still somewhat unsatisfactory, it can still be used after all,

Here, I will share my implementation methods and experiences.

Everything in it is available on the Internet, but here it is the most comprehensive.

Access the ACCESS database through VC/MFC

Method 1:

1. Create a dialog box-based application.

Put two ActiveX Controls: Microsoft ADO data control, version 6.0... and Microsoft DataGrid control. If version 6.0 does not have two spaces, install the office component.

 

2. Set Properties for the control.

1) Set Properties for ADO Data Control: Control-> Use connection string-> build

In the displayed dialog box, select Microsoft JET 4.0 ole db provider-> next-> in "select or enter database name", select the created database file, for example, D: /test_db/testdb. MDB. The password is blank by default. If the user table has a password, enter the password, select "allow Save Password"-> click "test connection". If "test connection succeeded" is displayed, the data file is ready for use. Click OK and return to the resource editing page of the dialog box.

Set the attributes for the recordsource option of this control as follows:

Select 1-ad1_text for command type.

In command text (SQL), enter the SQL statement: Select * From mydb;

The properties of the ADO Data Control control are set. Disable the Property setting interface.

2) Set Properties for the DataGrid Control.

Select idc_adodc1 from the datasource list.

Disable the Property setting interface.

Compile and run. If there is data in the database, it will be displayed in the DataGrid.

To perform other database operations, see the following example:

Next, the procedure above,

Add a variable m_adodc for ADO Data Control;

Add a variable for Data Grid: m_adodg;

A button. Double-click it to add a function. Add the following code to the function:

Cstring strtem = "select * From mydb where susername <> 'zhang san '";

// VC 6.0

M_adodc.setrecordresource (strtem );

M_adodc.refresh ();

// VS 2005/Vs 2008

M_adodc.put_recordsource (strtem );

M_adodc.refresh ();

 

Compile and run the program.

PS: In this mode, the query results are very good. However, some error messages may occur during the insert, update, and delete operations:

// In VC 6.0

The object is closed and cannot be operated.

// In

Windows has triggered a breakpoint in...

Although there are prompts, the database operation is successful. I have searched the network for this problem, and no one can provide a solution. Similarly, I am not better than them.

This is the end of the first solution.

 

From: http://blog.csdn.net/mrdone/article/details/4868741

The other day, I got a little thing to access the ACCESS database, and it took some effort to get it done.

Although it is still somewhat unsatisfactory, it can still be used after all,

Here, I will share my implementation methods and experiences.

Everything in it is available on the Internet, but here it is the most comprehensive.

Access the ACCESS database through VC/MFC

Method 1:

1. Create a dialog box-based application.

Put two ActiveX Controls: Microsoft ADO data control, version 6.0... and Microsoft DataGrid control. If version 6.0 does not have two spaces, install the office component.

 

2. Set Properties for the control.

1) Set Properties for ADO Data Control: Control-> Use connection string-> build

In the displayed dialog box, select Microsoft JET 4.0 ole db provider-> next-> in "select or enter database name", select the created database file, for example, D: /test_db/testdb. MDB. The password is blank by default. If the user table has a password, enter the password, select "allow Save Password"-> click "test connection". If "test connection succeeded" is displayed, the data file is ready for use. Click OK and return to the resource editing page of the dialog box.

Set the attributes for the recordsource option of this control as follows:

Select 1-ad1_text for command type.

In command text (SQL), enter the SQL statement: Select * From mydb;

The properties of the ADO Data Control control are set. Disable the Property setting interface.

2) Set Properties for the DataGrid Control.

Select idc_adodc1 from the datasource list.

Disable the Property setting interface.

Compile and run. If there is data in the database, it will be displayed in the DataGrid.

To perform other database operations, see the following example:

Next, the procedure above,

Add a variable m_adodc for ADO Data Control;

Add a variable for Data Grid: m_adodg;

A button. Double-click it to add a function. Add the following code to the function:

Cstring strtem = "select * From mydb where susername <> 'zhang san '";

// VC 6.0

M_adodc.setrecordresource (strtem );

M_adodc.refresh ();

// VS 2005/Vs 2008

M_adodc.put_recordsource (strtem );

M_adodc.refresh ();

 

Compile and run the program.

PS: In this mode, the query results are very good. However, some error messages may occur during the insert, update, and delete operations:

// In VC 6.0

The object is closed and cannot be operated.

// In

Windows has triggered a breakpoint in...

Although there are prompts, the database operation is successful. I have searched the network for this problem, and no one can provide a solution. Similarly, I am not better than them.

This is the end of the first solution.

 

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.