VC + + Use ActiveX controls to connect and manipulate databases

Source: Internet
Author: User

VS2008 There is always a memory release error when using ActiveX controls to connect and manipulate the database, as if the use of VC6.0 will generate more than 2 files, there will be no problem.

VC + + Use ActiveX controls to connect and manipulate databases in two ways:

1. Connect to the database through the Properties window
2. Connect to the database through code

1. Connect to the database through the Properties window
Basic steps:
(1) Create a dialog-based application activexdb;

(2) Add CADODC Control: Right-click on the dialog box, select Insert ActiveX Control menu item, select Microsoft ADO Data Control version 6.0 in the popup dialog box, click "OK" button, A CADODC control was added successfully.

(3) using the CADODC control: you first need to connect to the database and then pass the SQL Statement Action data table. CADODC Control Right-click on the property, control, tap use connection string, set it with build. RecordSource Select 1-adcmdtext, the following box enters the SQL execution statement.

(4) Add the DataGrid control: Right-click on the dialog box, select the Insert ActiveX Control menu item, select Microsoft DataGrid Control version 6.0 in the Pop-up dialog box, click the "OK" button, Successfully added a DataGrid control.

(5) Use the DataGrid control to display DataSet data: The DataGrid control is typically used with the CADODC control or ADO object Rescordset. Because the CADODC control is created above, it can be bound to the Cadodc object through the DataGrid control's Properties window. Right-click the DataGrid control, point to the DataGrid Object menu item, open the Properties window, select the "All" tab, select the DataSource property, click in the Value column, and select the ID number of the CADODC control in the combo box that appears. In this way, the DataGrid control is associated with the CADODC control, which means that the DataGrid control can display the dataset data corresponding to the CADODC control.


2. Connect to the database through code

Basic steps
(1) Create a dialog-based application activexdb;
(2) Add the CADODC control, establish the corresponding control variable m_adodc; Add the DataGrid control, set the corresponding control variable m_adodc; property is not set;
(3) The dialog box where the control is located, the corresponding CPP file in the OnInitDialog () function, add the following code:
BOOL Cactivexdbdlg::oninitdialog ()
{
......
M_adodc. Setconnectionstring ("Provider=SQLOLEDB.1; Persist Security Info=false; User id=sa;initial Catalog=suntest;data source=www-ed1f301e78b; Persist Security info=false ");
CString sql = "SELECT * FROM warehouse 3 ORDER by warehouse number DESC";
M_adodc. Setrecordsource (SQL);
M_adodc. Refresh ();

M_grid. Setrefdatasource (M_ADODC. GetControlUnknown ());
......
return TRUE;
}

Note:
Much of what is written on the internet and in books is m_adodc. GetRecordSet (), with VC + + Compile error prompt: Cannot convert parameter 1 from ' Class c_recordset ' to ' struct IUnknown * '
Cause: M_ADODC. The return value of GetRecordSet () is the C_recordset type, and M_grid. Setrefdatasource (IUnknown *) requires an incoming parameter to be a IUnknown struct, causing a type mismatch error.

VC + + Use ActiveX controls to connect and manipulate databases

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.