Use the cdatabase class to read access databases

Source: Internet
Author: User
Tags odbc connection

// The following is the main code
Void creaddbdlg: onread ()
{
// Todo: add your control notification handler code here

Cdatabase database;
Cstring sqlstring;
Cstring scatid, scategory;
Cstring sdriver = "Microsoft Access Driver (*. mdb )";
Cstring sdsn;
Cstring sfile = "C: // works // readdb // test. mdb"; // change path here
Int IREC = 0;
 
// Create ODBC connection dinamically
Sdsn. Format ("ODBC; driver = {% s}; DSN =''; DBQ = % s ", sdriver, sfile );
Try
{
// Open the database
Database. Open (null, false, false, sdsn );

// Allocate the recordset
Crecordset recset (& database );

// Build the SQL statement
Sqlstring = "select catid, category"
"From categories ";

// Execute the query
Recset. Open (crecordset: forwardonly, sqlstring, crecordset: readonly );
Resetlistcontrol ();
 
// Column width and heading
M_listcontrol.insertcolumn (0, "Cat ID", lvcfmt_left,-1, 0 );
M_listcontrol.insertcolumn (1, "category", lvcfmt_left,-1, 1 );
M_listcontrol.setcolumnwidth (0, 70 );
M_listcontrol.setcolumnwidth (1,200 );

// Loop through each record
While (! Recset. iseof ())
{
// Copy each column into a variable
Recset. getfieldvalue ("catid", scatid );
Recset. getfieldvalue ("category", scategory );

// Insert values into the List Control
IREC = m_listcontrol.insertitem (0, scatid, 0 );
M_listcontrol.setitemtext (0, 1, scategory );

// Goto next record
Recset. movenext ();
}
// Set column width
// M_listcontrol.setcolumnwidth (0, lvscw_autosize );
// M_listcontrol.setcolumnwidth (1, lvscw_autosize );
// Close the database
Database. Close ();
}
Catch (cdbexception, E)
{
// If a database exception occured, Show ERROR msg
Afxmessagebox ("Database Error:" + E-> m_strerror );
}
End_catch;
}
 

Void creaddbdlg: resetlistcontrol ()
{
M_listcontrol.deleteallitems ();
Int inbrofcolumns;
Cheaderctrl * pheader = (cheaderctrl *) m_listcontrol.getdlgitem (0 );
If (pheader)
{
Inbrofcolumns = pheader-> getitemcount ();
}
For (INT I = inbrofcolumns; I> = 0; I --)
{
M_listcontrol.deletecolumn (I );
}
}

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.