Skip to Content.
Sympa Menu

freetds - Re: [freetds] connect to SQL server from linux example

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Jeff Dahl <jddahl AT micron.com>
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Subject: Re: [freetds] connect to SQL server from linux example
  • Date: Wed, 20 Sep 2006 11:02:47 -0600

Here's the basic idea. I found MS ODBC reference <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcodbc_api_reference.asp> a great resource.

#include "sqlext.h"
#define MAX_OUT_CONNECTION_STRING_LEN 1024

SQLHDBC connection;
unsigned char outConnectionString[MAX_OUT_CONNECTION_STRING_LEN];
SQLSMALLINT len;
SQLHSTMT cmd;
SQLRETURN ret;
SQLHENV context;

ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &context);
// check ret value;

ret = SQLAllocHandle(SQL_HANDLE_DBC, context, &connection);
// check ret value;

ret = SQLAllocHandle(SQL_HANDLE_STMT, connection, &cmd);
// check ret value;

ret = SQLDriverConnect(connection, NULL, (SQLCHAR *) "DRIVER=FreeTDS;SERVER=[your server name];UID=[your user ID];PWD=[your password];APP=[your application];WSID=[your hostname];Port=[server port];TDS_Version=[the TDS version the server uses]", SQL_NTS, outConnectionString, MAX_OUT_CONNECTION_STRING_LEN, &len, SQL_DRIVER_NOPROMPT);
// check ret value;

ret = SQLExecDirect( cmd, (unsigned char *)"[your sql statement]", SQL_NTS );
// check ret value;

/* Bind the columns */
ret = SQLBindCol( cmd, [column number], [column data type], [buffer], [max length], [address of indicator] );
// check ret value;
.
.
.

while(( ret = SQLFetch( cmd )) != SQL_NO_DATA )
{
printf( "%s", buffer );
}

ret = SQLDisconnect( connection );
// check ret value;

ret = SQLFreeHandle( SQL_HANDLE_DBC, connection );
// check ret value;

ret = SQLFreeHandle( SQL_HANDLE_ENV, context );
// check ret value;





Archive powered by MHonArc 2.6.24.

Top of Page