Direct SQL Calls With ODBC and STL
Posted
by Ian Duff
on January 3rd, 2002
- The CPtrArray has been replaced by a standard STL vector.
- The Column classes have been coded to orthodox canonical form of class for STL usage.
- Many of the ODBC calls are deprecated and have been replaced with later ODBC function calls.
- The demo project contains some example code usage.
- Worthwhile reader contributions have been applied to the code.
The code and project require a DSN to the MS Northwind sample database in order to run correctly and this short article is offered as a technique for using simple STL container classes and ODBC.
Thanks to all at codeguru for sharing code and ideas.
Example code usage also available in the demo project:
try {
cout << "Connecting to Sample Northwind database..." << endl;
CIDSQLDirect sqlDirect; // Our direct ODBC class
if (( nRetCode = sqlDirect.Connect("Northwind") ) != SQL_SUCCESS )
return( nRetCode );
if (( nRetCode = sqlDirect.ExecuteSQL("SELECT COUNT(*) FROM Employees"))
!= SQL_SUCCESS )
return( nRetCode );
if (( nRetCode = sqlDirect.Fetch() ) != SQL_SUCCESS )
return( nRetCode ); // Fetch the result from this COUNT(*)
string strFetch = sqlDirect.GetCol( 1 );
cout << "Employees Table contains (" << strFetch << ") entries" << endl;
// This query returns all data in the Employees table
if (( nRetCode = sqlDirect.ExecuteSQL("SELECT * FROM Employees"))
!= SQL_SUCCESS )
return( nRetCode );
int nColumns = sqlDirect.GetNumColumns();
cout << "Query returned (" << nColumns << ") columns" << endl;
sqlDirect.PrintColumnNames();
DisplayData( sqlDirect ); // Print the information now contained
}
catch( ... ) {
cout << "SQL Exception Caught!" << endl;
}
Downloads
Download demo project - 26 KbDownload source - 9 Kb

Comments
There are no comments yet. Be the first to comment!