VC ODBC Summary

xiaoxiao2021-03-05  24

1. Open the database

CDatabase.openex; Database.openex (_T ("DSN = Zhuxue"), cdatabase :: noodbcdialog; // zhuxue is the data source name

2. Association record set

CRecordset Recset (& Database);

3. Query record

CString SSQL1 = ""; ssql1 = "Select * from tablename"; recset.open (CRecordset :: Forwardonly, SSQL1, CRecordset :: readonly);

INT TI = 0; cdbvariant var; // var can be converted to other types of values

While (! recset.iseof ()) {// reads Excel internal value recset.GetfieldValue ("ID", var); jiangxiang [ti] .id = var.m_ival; recset.getfieldValue ("name", jiangxiang [TI ] .name); Ti ; recset.movenext ();

Recset.close (); // Close Record Set

4. Execute SQL statements

Cstring ssql = ""; ssql = "delete * from hospital system audit"; // Empty table Database.executeSQL (SSQL);

SSQL can also be an insert, update and other statements

5. Read the field name

SSQL = "Select * from sheet1"; // read the file with the definition of the sheet1 table, or the table generated for this program. // Execute Query Statement RecSet.open (CRecordset :: Forwardonly, SSQL, CRecordset :: Readonly) INT excelcolcount = recset.getodbcfieldcount (); // column number CString Excelfield [30]; // Get the total number of fields in the field collection of record sets FOR (i = 0; I

6. Open an Excel file

CString SDRIVER = "Microsoft Excel Driver (* .xls)"; // Excel Install Drive CString SSQL, Sexcelfile; // Sexcelfile for the file path for Excel

Try {// Create a string SSQL.Format ("driver = {% s}; DSN = '; firstonly = false; recreate_db = /"% s / "; dbq =% s" , SDriver, Sexcelfile, Sexcelfile; // Create a database (both EXCEL Table File) IF (Database.openex (SSQL, CDATABASE :: NoodbcDialog) {

// You can operate Excel as a database

}

转载请注明原文地址:https://www.9cbs.com/read-32346.html

New Post(0)