Solved

Getting started using SQL Native Client API in C code

Posted on 2008-06-15
3
769 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

732 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