[freetds] Example C Application

Reid, Roger L. roger.reid at dpw.com
Fri Dec 7 11:11:03 EST 2007


Your best bet is to look at Sybase's dblib documentation.

I do find I need to reorganize my makefiles when switching from Sybase's dblib to FreeTDS, something about the 
dependency interactions, but that will likely differ from system to system.

Here's a very basic program: it is NOT an example of particularly good practice, but I didn't want to bog it down much
in the example.   Check the Sybase documentation or SQL Server, look for dblib.
========================================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <sybfront.h>
#include <sybdb.h>

static LOGINREC              *src_login;
static DBPROCESS             *src_dbproc;

main(int argc, char *argv[]) {

	RETCODE retcode;
	int seqnbr_arg = 99;
	char Name[500], Desc[500];
	int Value;
        char *src_serv = "MYSERVERNAME"; /* as listed in freetds.config */

	if (dbinit() == FAIL)
		exit(-1);

	if((src_dbproc = dbopen(src_login, src_serv)) == FAIL) {
		fprintf(stderr, "Could not open server %s\n", src_serv);
		exit(1);
	}

	dbfcmd(src_dbproc, 
               "SELECT p.name, p.value, c.desc FROM mydb.dbo.ptable AS p JOIN mydb.dbo.ctable AS c ON p.seqnbr = c.seqnbr where c.seqnbr = %d", 
               seqnbr_arg);
	
	dbsqlexec(src_dbproc);

	*Name = *Desc = '\0';
        Value = 0;
 	*DestPasswd = *whereclause = *sortby = '\0';
	/* dbresults will return once for every query in a batch (often 1) before returning NO_MORE_RESULTS */
	while (dbresults(src_dbproc) != NO_MORE_RESULTS) {
		dbbind(src_dbproc, 1, STRINGBIND, 0, (BYTE *)Name);
		dbbind(src_dbproc, 2, INTBIND, 0, (BYTE *)&Value);
		dbbind(src_dbproc, 3, STRINGBIND, 0, (BYTE *)Desc);
		/* dbnextrow will be called once per row in the query before returning NO_MORE_ROWS */
		while (dbnextrow(src_dbproc) != NO_MORE_ROWS) {
			/* do RowByAgonizingRow processing */
		}
	}
	
	dbclose(src_dbproc);
	return 0;
}



-----Original Message-----
From: pedram at pr-sol.com [mailto:pedram at pr-sol.com] 
Sent: Thursday, December 06, 2007 1:14 AM
To: freetds at lists.ibiblio.org
Subject: [freetds] Example C Application


Hi,

I am currently having some difficulties writing and compiling
a C application after installing the freetds package.

Anybody have any reference to a very basic C application which
connects to a SQL Server and runs a single query?

Thanks,
Pedram




More information about the FreeTDS mailing list