Visual c ++ ODBC parameterized record set Query Design

Source: Internet
Author: User

ODBC automatically exchanges data between data sources and record set objects through rfx, and binds the record set data to the form control through data exchange in the DDX dialog box. This article provides a simple query using the getdefasql SQL method. However, the query statement here is set, the flexibility is not high, and the relationship between each condition is or, complex queries cannot be implemented.

Before using a parameterized record set, you must determine the required parameters. The data table used for learning is as follows:

Set all fields as parameters, define parameters in the record set class, and initialize them:

// Parameter DefinitionLongM_stunum2; cstringwm_name2; cstringwm_gender2;LongM_age2;LongM_classnum2;
// Initialization in the constructor
M_stunum2 = 0; m_name2 = l"*"; M_gender2 = l"*"; M_age2 = 10; m_classnum2 = 1; m_nparams = 5;

Add the parameter data exchange function to dofieldexchange:

 Void Cdatatestset: dofieldexchange (cfieldexchange * pfx) {pfx-> setfieldtype (cfieldexchange: outputcolumn ); // Macros such as rfx_text () and rfx_int () are dependent on  // Type of the member variable, not the type of the field in the database.  // ODBC will try to automatically convert the column value to the requested type Rfx_long (pfx, _ T (" [Student ID] "), M_stunum); rfx_text (pfx, _ T (" [Name] "), M_name); rfx_text (pfx, _ T (" [Gender] "), M_gender); rfx_long (pfx, _ T (" [Age] "), M_age); rfx_long (pfx, _ T (" [Class number] "), M_classnum); pfx-> setfieldtype (cfieldexchange: inputparam); rfx_long (pfx, _ T (" 1" ), M_stunum2); rfx_text (pfx, _ T (" 2 "), M_name2); rfx_text (pfx, _ T (" 3 "), M_gender2); rfx_long (pfx, _ T (" 4 "), M_age2); rfx_long (pfx, _ T (" 5 "), M_classnum2 );}

Sets the filter string for the query condition. The parameter placeholder (?) in the string (?) It is consistent with the binding order above:

Cstring cdatatestset: getdefasql SQL () {m_strfilter = l"Student ID =? Or name =? Or gender =? Or age =? Or class number =?";Return_ T ("[Student information table]");}

Finally, set data exchange in the View class and write the corresponding query function:

 Void Cdatatestview: dodataexchange (cdataexchange * PDX) {crecordview: dodataexchange (PDX ); // You can insert the ddx_field * function here to "Connect" the control to the database field, for example  // Ddx_fieldtext (PDX, idc_myeditbox, m_pset-> m_szcolumn1, m_pset );  // Ddx_fieldcheck (PDX, idc_mycheckbox, m_pset-> m_bcolumn2, m_pset );  // For more information, see the msdn and ODBC examples Values (PDX, idc_edit1, m_pset-> m_stunum, m_pset); values (PDX, idc_edit2, m_pset-> m_name, m_pset); ddx_fieldtext (PDX, idc_edit3, m_pset-> m_gender, m_pset); ddx_fieldtext (PDX, idc_edit4, m_pset-> m_age, m_pset); ddx_fieldtext (PDX, idc_edit5, m_pset-> m_classnum, m_pset ); // Dialog box data exchange Ddx_text (PDX, idc_edit6, m_stunum); ddx_text (PDX, idc_edit7, m_name); ddx_text (PDX, example, m_sex); ddx_text (PDX, idc_edit9, m_age); ddx_text (PDX, idc_edit10, m_classnum );} Void Cdatatestview: onmyquery () {updatedata (true); m_pset-> attributes = m_classnum; m_pset-> m_name2 = m_name; m_pset-> m_gender2 = m_sex; m_pset-> m_stunum2 = m_stunum; m_pset-> m_age2 = m_age; m_pset-> requery (); updatedata (false );}

The final result is as follows:

Related Article

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: 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.