Skip to Content.
Sympa Menu

freetds - Re: [freetds] Hello World help

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Kenneth Knowles <kknowles AT berkeley.edu>
  • To: freetds AT lists.ibiblio.org
  • Subject: Re: [freetds] Hello World help
  • Date: Tue, 16 Mar 2004 13:29:37 -0800


Sorry, forgot to attach the files, here they are.

#include <sqlfront.h>
#include <sqldb.h>

void failwith ( char* err )
{
printf("%s\n", err);
exit( 1 );
}

int main(int argc, char* argv[])
{
/* Note: page "32" - sybase thinks it is 20 - has the steps in a
simple program */
LOGINREC* login;
DBPROCESS* proc;

/* Init */
if ( dbinit() != SUCCEED ) failwith ( "dbinit() failed");
login = dblogin();


/* Connect */
DBSETLUSER(login, "USER");
DBSETLPWD(login, "PWD");
proc = dbopen( login, "SERVER" );

/* Prep a command and run it */
dbcmd(proc, "select top 10 COLUMN from DB..TABLE");
if ( dbsqlexec(proc) != SUCCEED ) failwith ( "dbsqlexec() failed" );

/* Loop through the recordset */
while( dbresults(proc) == SUCCEED )
{
char string[100];
printf( "Column named %s\n", dbcolname(proc, 1) );

if ( dbbind(proc, 1, STRINGBIND, -1, (BYTE*)string) !=
SUCCEED ) failwith("dbbind() failed");

while( dbnextrow(proc) != NO_MORE_ROWS )
printf("%s\n", string);
}

dbloginfree(login);
dbclose(proc);
dbexit();
}
#include <ctpublic.h>

int main(int argc, char* argv[])
{
/* Note: page 32 in sybase pdf has the steps in a simple program */
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
CS_COMMAND *cmd;
CS_RETCODE ret;
CS_RETCODE restype;
CS_DATAFMT datafmt[10];

/* First we need a "context" whatever that is */
ret = cs_ctx_alloc(CS_VERSION_100, &ctx);
/* Then some global initialization */
ret = ct_init(ctx, CS_VERSION_100);

/* Connect */
ret = ct_con_alloc(ctx, &conn);
ret = ct_con_props(conn, CS_SET, CS_USERNAME, "USER", CS_NULLTERM,
NULL);
ret = ct_con_props(conn, CS_SET, CS_PASSWORD, "PASSWORD",
CS_NULLTERM, NULL);
ret = ct_connect(conn, "SERVER", CS_NULLTERM);

/* Prep a command */
ret = ct_cmd_alloc(conn, &cmd);
ret = ct_command(cmd, CS_LANG_CMD,
"select top 10 COLUMN from DB..TABLE",
CS_NULLTERM, CS_UNUSED);
if (ret != CS_SUCCEED) {
fprintf(stderr, "ct_command() failed\n");
exit(1);
}

/* Run the command */
ret = ct_send(cmd);
if (ret != CS_SUCCEED) {
fprintf(stderr, "ct_send() failed\n");
exit(1);
}

/* Loop through the recordset */
while( ct_results(cmd, &restype) == CS_SUCCEED )
{

printf("got restype %d (we want %d) \n", restype,
CS_ROW_RESULT);

if ( restype == CS_ROW_RESULT )
{
CS_SMALLINT ind;
CS_INT len, rows_read, numcols, rows_affected;
CS_DATAFMT output;
char string[100];


/* I think the param to ct_bind is a "1" index */
/* This is stuff to get the thing ready for reading */
if ( ct_res_info(cmd, CS_ROW_COUNT, &rows_affected,
CS_UNUSED, NULL) != CS_SUCCEED )
{
printf( "ct_res_info( ) failed\n" );
exit ( 1 );
}

ct_describe( cmd, 1, &output );
printf( "Column named %s\n", output.name );
if ( output.datatype != CS_CHAR_TYPE ) {
printf( "Non-char type %d", output.datatype
); exit( 1 );
}
if ( ct_bind(cmd, 1, &output, &string, &len, &ind) !=
CS_SUCCEED ) {
printf( "ct_bind( ) failed\n"); exit(1);
}

while( ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED,
&rows_read ) == CS_SUCCEED )
{
printf("%s\n", string);
};

}
}

ct_cmd_drop(cmd);
ct_close(conn, CS_UNUSED);
ct_exit(ctx, CS_UNUSED);
cs_ctx_drop(ctx);

}



Archive powered by MHonArc 2.6.24.

Top of Page