[freetds] connect to SQL server from linux example
Jeff Dahl
jddahl at micron.com
Wed Sep 20 13:02:47 EDT 2006
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;
More information about the FreeTDS
mailing list