ODBC – SQLBindCol Function – Using Microsoft’s sample code – Code Review

Background

A quick follow-up to our original post on using the ODBC SQLBindCol function.

Lineage

  1. ODBC – SQLBindCol Function – Using Microsoft’s sample code
    Date Published :- 2020-01-01
    Link

Microsoft Sample

Web Site

Tried out Microsoft’s sample; available here.

Docs / SQL ODBC / ODBC Advanced / Microsoft Open Database Connectivity (ODBC)/  ODBC Programmer’s Reference / API Reference / ODBC API Reference / SQLBindCol Function

SQLBindCol Function
Link

Code Review

Outline

  1. Header Files
  2. ODBC Data Source
    • NorthWind
  3. Database Query
    • SQL
      • SELECT CustomerID, ContactName, Phone FROM CUSTOMERS
  4. Error Handling
    • Original
      • void show_error() {
        printf(“error\n”);
    • Revised
      • easysoft.com
        • ODBC Diagnostics & Error Status Codes Contents
          Link
  5. Execute SQL
    • Original
      • SQLExecDirect
    • Revision
      • SQLExecDirectW
  6. Bind Column ( SQLBindCol  )
    • Arguments
      • Argument – TargetType
        • Original
          • SQL_C_CHAR
        • Revision
          • SQL_C_WCHAR

Items

Header Files

  1. windows.h
  2. stdio.h
  3. sqlext.h
    • ODBC Functions
      • #define UNICODE
        • Indicate we are interested in Unicode databases, as well

ODBC Data Source

  1. NorthWind
    • Hardcoded to expect and use a specific Data Source NorthWind

Database Query

  1. SQL
    • SELECT CustomerID, ContactName, Phone
      FROM CUSTOMERS

Error Handling

Original

errorHandling.original.01.20200102.0934PM

Revision

easysoft – Extract_error
Link
Extract_error is available :-

Retrieving ODBC Diagnostics
Link

Image

errorHandling.extract_error.01.20200102.0936PM

Self

 

Bind Column ( SQLBindCol  )

Outline

  1. For unicode columns
    • Arguments
      • Argument – TargetType
        • Please change SQL_C_CHAR to SQL_C_WCHAR
      • Argument – StrLen_or_Ind
        • For unicode columns the strlen_Or_Ind will be double the actual displayed length

Original

sqlBindCol_original_01_20200102_0957PM.PNG

Revision

Image

sqlBindCol_revision_01_20200102_0956PM

Display Data ( printf, wprintf )

Outline

  1. wprintf
    • Changes
      • Please change %S to %ls
    • Code
      • Original
        • wprintf(L”%d: %S %S %S\n”, i + 1, sCustID, szName, szPhone);
      • Revision
        • wprintf(L”%d: %ls %ls %ls\n“, i + 1, sCustID, szName, szPhone);

Original

display_data_origianal_01_20200102_1011PM

Revision

Image

display_data_revision_01_20200102_1014PM.PNG

 

Source Code

GitHub

  1. Repository
    • DanielAdeniji/ODBCCodeSample
      Link
    • Files
      • Vendor
        • northwind.SQLBindCol_ref.vendor.c
          Link
      • Revisions
        • northwind.SQLBindCol_ref.vendor.revised.c
          Slight Revision
          Link
        • northwind.SQLBindCol_ref.c
          Multiple Revision
          Link

Database

Artifacts

Northwind

SQL Server Sample Databases are available via GitHub.

Here are the links :-

  1. microsoft/sql-server-samples
    Link
  2. sql-server-samples/samples/databases/
    Link
  3. sql-server-samples/samples/databases/northwind-pubs/
    Link
  4. Northwind

Deployment

Please download instnwnd.sql and run via SQL Server Management Studio or alternate SQL Server Client.

 

ODBC Data Source

The software code relies on an ODBC Data Source called northwind.

Please use an ODBC Manager to deploy.

Depending on the bitness of your application, please choose one of the ODBC Managers listed below.

  1. odbc x32
    %windir%\syswow64\odbcad32.exe
  2. odbc x64
    %windir%\system32\odbcad32.exe

 

Dedicating

Dedicating to Microsoft’s Gene Milener.

Made a couple of changes outlined here.

The changes were made to GitHub.

First thing 7:25 AM on Thursday, January 2nd, nodoubt, he picked up the changes and merged them into mainline.

GeneMilener_01_20200102_1021PM.PNG