Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting started using SQL Native Client API in C code

Posted on 2008-06-15
3
Medium Priority
?
781 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

604 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