Over a million developers have joined DZone.

Going Native with SQL Server – Introduction

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Coucbase.

This is the first in a series of blog posts I’m planning to do, in preparation for a potential SQLBits session in March 2012.

This article will introduce how, at the most basic level, SQL Server can be communicated with using C++ and Native code, rather than using the .NET Framework.

The code shown below follows through the basic process, defined in the general flowchart for ODBC Applications as seen on MSDN. This was created in VS2010.

Using this process, we connect to a server (a local copy of SQL Server, with AdventureWorks 2008 R2, and does a straightforward query against it to do a Row Count of the Person table.

// The bare basics to query SQL Server, using the Native Client, in C++
#include "stdafx.h"
#include <iostream>
using namespace std;

#define _SQLNCLI_ODBC_
#include "sqlncli.h"
#include "sqlext.h"

int _tmain(int argc, _TCHAR* argv[])
    // Define Handles
    SQLHANDLE hEnv, hDBCCount, hStmtCount;
    SQLINTEGER iRowCount, iRowCountInd;

    char sConnString[120] = "Driver={SQL Server Native Client 10.0};Server=localhost;Database=AdventureWorks2008R2;Trusted_Connection=yes;";

    // Step 1 - Assigning an Environment Variable

    // Step 1 - Declaring the use of ODBCv3

    // Step 1 - Creating a Connection Handle
    SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDBCCount);

    // Step 1 - Setting Connection Attributes

    // Step 1 - Initiating the connection to SQL Server
    SQLDriverConnect(hDBCCount, NULL, (SQLTCHAR *) sConnString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);

    // Step 2 - Creating a Handle for the Statement
    SQLAllocHandle(SQL_HANDLE_STMT, hDBCCount, &hStmtCount);

    // Step 3 - Connecting to AdventureWorks2008R2
    SQLExecDirect(hStmtCount, (SQLTCHAR *)"USE AdventureWorks2008R2;", SQL_NTS);
    cout << "USE AdventureWorks2008R2;" << endl;

    // Step 3 - Executing Query against Person.Person table
    SQLExecDirect(hStmtCount, (SQLCHAR *)"select count(1) from Person.Person;" , SQL_NTS);
    cout << "select count(1) from Person.Person;" << endl;

    // Step 4a - Assigning a variable to the return column
    SQLBindCol(hStmtCount, 1,SQL_C_ULONG, &iRowCount, 0, &iRowCountInd);

    // Step 4a - Retrieving the data from the return dataset

    cout << "Rows = " << iRowCount << endl;

    // Step 4a - Remove the Cursor

    // Step 5 - Closing down and Cleaning up
    SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

    return 0;

Over the coming weeks, I’ll be expanding on this to get better performance for more complex processes and going through what each of these sections to.

I hope you find these articles interesting.

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.


Published at DZone with permission of Nick Haslam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}