Solved

Getting started using SQL Native Client API in C code

Posted on 2008-06-15
3
763 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
Comment Utility
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
Comment Utility
>>> 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
Comment Utility
Thanks, have compiled with the SQL.H and some preliminary function calls, so it looks like it works!

Thanks again
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now