Solved

Getting started using SQL Native Client API in C code

Posted on 2008-06-15
3
765 Views
Last Modified: 2010-04-21
HI,.
I have written platform independent C code (for linux and windows) and would like to use the APIs for MS SQL Server so that I can connect to any SQL Servers when necessary.

I have no idea where to start...Which header files to include and what the function names are to connect to a database, etc

I compile in either Borland C++ Builder or MS Visual Studio 6 C++ ...

A code snippet and compiler settings would be great, so I can learn:
- Where the h files and lib/dll files are for the SQL NAtive client
- What function reference help is available

Thanks
0
Comment
Question by:gmleeman
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 21789278
One promising approach could beODBC, since it's available for a variety of platforms. See

http://www.unixodbc.org/
http://www.iodbc.org/
http://support.microsoft.com/kb/110093
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 21789365
>>> Which header files to include and what the function names are to connect to a database, etc

When using ODBC as jkr suggested you mostly could use independent code.

#include <sql.h>

That is the only include you need for basic ODBC access.

To connect to a database you firstly need to install a ODBC client for your DBMS. At Windows the SQLServer ODBC client was installed with the Visual Studio (and some other IDE's as well). You call odbcad32 from Start-Run to define a 'datasource'. As far as I remember, you add a datasource at Linux by editing a config file (perhaps odbc.ini).

Programmatically the connect was done by calling SQLConnect. Before calling you need a SQLHENV (handle to environment) which you get by calling SQLAllocEnv, then a SQLHDBC (handle to db connection) which you get by calling SQLAllocConnect.

SQLHENV  henv = 0;
SQLHDBC hdbc = 0;
SQLRETURN  ret = 0;

ret = SQLAllocEnv(&henv);
ret = SQLAllocConnect(henv, &hdbc);

ret =  SQLConnect(hdbc, "data_source", SQL_NTS, NULL, 0, NULL, 0);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
     ...

Of course any return should be tested on SQL_SUCCESS.
0
 
LVL 3

Author Closing Comment

by:gmleeman
ID: 31467377
Thanks, have compiled with the SQL.H and some preliminary function calls, so it looks like it works!

Thanks again
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show Results for Latest DateTime in a View 27 27
SQL View nearest date 5 39
Help in Bulk Insert 9 38
Syntax for query to update table 2 16
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question