Classes for direct SQL calls with ODBC
During my current project, I found that I needed more functionality with my sql calls than CRecordset or CDAORecordset had to offer. These classes wrap the low-level ODBC API calls and act simular to a CRecordset, but allow me to execute any SQL statement without having to bind variables,etc.
The main class for doing this is CSQLDirect. CSQLDirect has the following attributes/functions:
- CSQLDirect::Connect - Connects to a datasource.
- CSQLDirect::ExecuteSQL - this is the main function that is used for handling the SQL statement.
- CSQLDirect::GetCol - Will return a column from a table in the resulting cursor.
- CSQLDirect::GetError - Provides detailed error messages in case something went wrong.
- CSQLDirect::GetColumnType - Provides information about a cursor's column.
- CSQLDirect::Fetch - Will properly execute a SQLFetch command with error handling.
- CSQLDirect::Close - Closes the connection to the datasource.
The other class CSQLColumn is a support class for CSQLDirect. Since multiple queries to a cursor's column will result in a NULL value being returned, I found it necessary to keep track of the columns as they are used. This is stored in a CPtrArray within CSQLDirect and is cleaned up after each time the cursor is requeried/closed.
An example of using this class to make SQL Calls:
CSQLDirect SQLDirect( "TestData" );
if( SQLDirect.ExecuteSQL( "SELECT * FROM Employee" )==SQL_SUCCESS ) {
while( SQLDirect.Fetch()==SQL_SUCCESS ) {
.
// Do some stuff here
.
}
}
That's it!
The great thing about this class is you no longer have need for a huge assortment of CRecordset classes for every table/query.
Anyways I hope this can be of help to anyone that uses the site. Don't hesitate to give me a shout if anyone has any questions/comments.
Thanks for all the help that CodeGuru has given my over the last few months!

Comments
GetCol bug
Posted by Legacy on 02/09/2004 12:00amOriginally posted by: Jay
-
ReplyGetCol bug
Posted by Miros on 06/25/2004 03:02pmI've found just the same, but only with MS SQL db connection. With MS Access db it works.
ReplyHow to use the "SQLColumns()" in Visual Basic
Posted by Legacy on 10/28/2003 12:00amOriginally posted by: Tony
I want to get the information of a table , but don't know how to use the "SQLColumns()" in Visual Basic.Can you tell me the method and thank a lot .If you can give one example,it's very wonderful!
ReplyHow to read and write binary data by ODBC API
Posted by Legacy on 07/14/2003 12:00amOriginally posted by: shenzheng
ReplyAnyone knows how to connect the mysql to my programme in Linux?
Posted by Legacy on 07/10/2003 12:00amOriginally posted by: Laux
Hi
I am a student in Hong Kong. I need to check the data stored in mysql (Linux) from my programme (written in C++). Do anyone knows how I can do it?
I have read the site www.mysql.com, but the site only told me some C API, but it didn't tell me which header file and object file needed to be includeed.... Can anyone help me?
ReplyThx
SELECT MAX Doesn't work!
Posted by Legacy on 05/25/2003 12:00amOriginally posted by: Ke
I have an error message when I use this SQL command:
SELECT MAX(ID_COL) FROM MY_TABLE;
Where is the problem? Does anyone have a solution?
ReplyWhat about LOBs ???
Posted by Legacy on 05/20/2003 12:00amOriginally posted by: d00guy
I'm trying to use blob and clob in my application but I encounter certain problems to make INSERT. I need some help !!
Thanks
d00guy
ReplyTHANK YOU -- Supports MySQL
Posted by Legacy on 03/06/2003 12:00amOriginally posted by: Mike Aerni
I had a b$(%tch of a time trying to get another library (mysql++) working, but kept getting all kinds of link errors and/or application crashing when executing mysql commands. Your library made it SO EASY!!!! The only change I had to make was to add username/password to the CSQLDirect constructor and the Connect method. Thank you, your work is very much appreciated.
ReplyAnd mySQL?
Posted by Legacy on 01/22/2003 12:00amOriginally posted by: Marian Trizuliak
Hi,
can you tell what about mySQL? I use it, so I want to know some info about a connection through ODBC with mySQL and using of this example for that connection and executing SQL commands. Thx.
ReplyHow to upload Excel and upload excel data in MSSQL
Posted by Legacy on 12/12/2002 12:00amOriginally posted by: Srikanth
How to upload Excel and upload excel data in MSSQL
ReplyGreat work !
Posted by Legacy on 11/18/2002 12:00amOriginally posted by: Jerry
It is very helpful and easy to use.
Very nice !
Jerry
Reply
Loading, Please Wait ...