
ODBCWrappers v1.22 A set of C++
classes to encapsulate ODBC on Windows
The classes provided are: CODBC::CHandle, CODBC::CEnvironment,
CODBC::CConnection, CODBC::CStatement & CODBC::CDescriptor.
CHandle provides a class based encapsulation of a SQLHANDLE handle and
the various ODBC v3 APIs which work on any SQLHANDLE type handle.
CEnvironment derives from CHandle and provides encapsulation of a ODBC
environment handle. This class allows configuration of ODBC for initial usage and
creation of connections to a database
CConnection derives from CHandle and provides encapsulation of a logical
connection to a database.
CStatement derives from CHandle and provides encapsulation of an ODBC "statement"
which allows execution of commands against a ODBC connection.
CDescriptor derives from CHandle and provides encapsulation of an ODBC "descriptor".
Finally ODBCWrappers provides a framework which provides accessor maps
similar to the macros provided by the built in Visual C++ ATL OLE DB Consumer
templates. These provide a simple mapping from code using OLE DB and these
templates to equivalent ODBC functionality. This should provide a easy migration
path if you want to move your OLE DB code to ODBC.
Similar to the OLE DB classes, both static accessors (CODBC::CAccessor)
and dynamic accessors (CODBC::CDynamicColumnAccessor) are supported.
In 2011 Microsoft decided to deprecate OLE DB and this decision was one of the driving reasons to implement ODBCWrappers
in the first place as the only ODBC classes available in Visual C++ are MFC specific
(CDatabase/CRecordSet etc) and in-line with Microsoft's strategy it really does
not make sense to restrict our usage of ODBC to MFC specific use cases going forward.
The development of ODBCWrappers took a number of months of hard work researching,
developing and testing and hopefully you should find them useful in your C++ database
programming efforts. In 2018 Microsoft decided to un-deprecate OLE DB!
Features
- Provides a simple C++ Header only module to easily encapsulate ODBC.
- Supports both ASCII and UNICODE API's of ODBC on Windows.
- Provides a complete wrapping for all the ODBC v3.8 functionality.
- Integrates with STL's string classes and the native Windows TCHAR string
data types for easier client coding.
- Should make it easier to use ODBC from non Windows platforms going forward.
The enclosed zip file contains the ODBCWrappers
source code and a VC 2017 console based application which demonstrates most of
the classes functionality. To run correctly, this sample requires that a PJ_Test
stored procedure is created from the included "Create PJ_Test SP.sql" script
against a local installed instance of the sample AdventureWorks 2008 sample
database from Microsoft.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Updates
v1.22 (15 December 2023)
- Updated module to remove usage of _if_exists by now using ODBCVER and
_ATL_MODULES preprocessor macro checks along with SFINAE.
v1.21 (12 May 2023)
- Updated copyright details
- Updated module to indicate that it needs to be compiled using
/std:c++17. Thanks to Martin Richter for reporting this issue.
v1.20 (4 March 2022)
- Updated copyright details
- Updated the code to use C++ uniform initialization for all variable
declarations.
- Updated the code to use std::[w]string::data method throughout. This means
that the code must now be compiled using /std:c++17.
v1.19 (3 July 2021)
- Added support for date and time datatypes: TIME_STRUCT / SQL_TYPE_TIME &
DATE_STRUCT / SQL_TYPE_DATE. Thanks to Serhiy Pavlov for providing this nice
addition.
- Updated copyright details.
v1.18 (13 October 2020)
- Fixed an issue in CHandle::GetDiagRecords and
CHandle::ValidateReturnValue where the code would not terminate a loop if
SQLGetDiagRec returns a value other than SQL_NO_DATA. Thanks to Serhiy
Pavlov for reporting this issue.
v1.17 (9 October 2020)
- Fixed a copy and paste error around the ODBC_COLUMN_ENTRY_LENGTH_STATUS
macro. Thanks to Serhiy Pavlov for reporting this issue.
v1.16 (20 September 2020)
- Fixed more Clang-Tidy static code analysis warnings in the code.
- Added a new SET_ODBC_PARAM_FOCUS macro to the code. This allows
the code framework to be used with Table Value Parameters (TVP). A TVP is
where a table is passed as a parameter to a stored procedure. To support
this new functionality the CAccessor::BindParameters method now takes a non const
CStatement parameter. Thanks to Serhiy Pavlov for providing this nice
addition.
- Reworked a number of the macros to embed suppressing various VC++
compiler warnings at source. This change means that client code does not
need to suppress these compiler warnings at their call sites if they are
aiming for clean compilation when using code analysis.
v1.15 (13 April 2020)
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.14 (2 March 2020)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.13 (19 December 2019)
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.12 (1 October 2019)
- Fixed a number of compiler warnings when the code is compiled with VS
2019 Preview
v1.11 (14 April 2019)
- Updated copyright details
- Updated the code to clean compile on VC 2019
v1.10 (28 November 2018)
- Updated copyright details
- Fixed a number of C++ core guidelines compiler warnings. These changes
mean that the code will now only compile on VC 2017 or later.
- Removed code which supported CODBCWRAPPERS_MFC_EXTENSIONS define
v1.09 (3 December 2017)
- Replaced NULL throughout the codebase with nullptr. This means that the
minimum requirement for the framework is now VC 2010.
- Replaced BOOL throughout the codebase with bool.
- Replaced CString::operator LPC*STR() calls throughout the codebase with
CString::GetString calls
- Renamed CHandle::Cancel method to CHandle::CancelHandle
- Fixed problems in CAccessor::BindColumns and CAccessor::BindParameters
with the use of __if_exists and /permissive-.
- Fixed problems in the MFC code path of
CDynamicColumnAccessor::BindColumns with the use of "m_ColumnIndicators" and
/permissive-
v1.08 (29 April 2017)
- Updated copyright details.
- Updated the code to compile cleanly using /permissive-.
v1.07 (5 November 2015)
- Updated code to compile cleanly on VC 2015.
- Fixed compiler warnings in CHandle::SetAttr, CHandle::SetAttrU, CConnection::SetAttr,
CConnection::SetAttrU, CStatement::SetAttr & CStatement::SetAttrU related
to incorrect reinterpret_cast's.
- Implemented support for SQLCancelHandle via CHandle::Cancel.
- Implemented support for SQLCompleteAsync via CHandle:CompleteAsync
- Implemented support for setting attributes in CCommandBase::SetAttributes,
CCommandBase::Create & CCommand::Open. Thanks to Serhiy Pavlov for providing
this new feature.
v1.06 (14 March 2015)
- Added SAL annotations to all the code.
- Renamed the parameters to the methods of the classes to be consistent with
the parameter names in the latest Visual Studio 2013 header files.
v1.05 (27 February 2015)
- Updated copyright details.
- Addition of a new CCommand class based on the ATL class of the same name.
This class should make it easier to port ATL OLEDB code which is currently using
ATL::CCommand. Thanks to Serhiy Pavlov for requested this update.
v1.04 (21 February 2013)
- Addition of a new CODBCWRAPPERS_MFC_EXTENSIONS preprocessor value which
changes the wrapper classes to more tightly integrate with MFC. Internally the
classes will then use the MFC collection classes and expose a CString interface
- Following a customer request the code should now be compilable in VC 6 for
those diehards still stuck on this compiler.
v1.03 (19 February 2013)
- Updated copyright details.
- Updated code to provide default sensible values for the StrLen_or_IndPtr
parameter if it is not explicitly provided by client code.
- Updated the sample app to shown examples of inserting into a table, iterating
across the rows in a table, updating a row in a table and deleting a row from
a table.
- Removed some accidental usage of MFC TRACE and ASSERT functions and replaced
with ATL equivalents.
- Pulling in MS SQL Server extensions header file sqlext.h and associated
functionality is now optional via a new CODBCWRAPPERS_MSSQL_EXTENSIONS preprocessor
value
- Sample app now does not link against MFC for demonstration purposes
v1.02 (20 November 2011)
- Removed final occurrence of std::auto_ptr usage in CStatement::GetCursorName.
v1.01 (16 November 2011)
- Updated code to use std::vector instead of std::auto_ptr for allocating
SQLTCHAR temp heap arrays. Thanks to Andrey Karpov for reporting this issue.
- BrowseConnect method which returns a string& parameter now uses a TCHAR*
parameter instead of a SQLTCHAR* parameter for the first parameter
v1.0 (13 November 2011)