Solved

Getting started using SQL Native Client API in C code

Posted on 2008-06-15
3
772 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.

705 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