程序员人生 网站导航

在linux下通过C语言基于freetds连接sql2000

栏目:服务器时间:2013-10-05 03:02:30
安装好freetds之后
添加库的环境变量
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/
之后开vi
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include </usr/local/freetds/include/sybfront.h>
#include </usr/local/freetds/include/sybdb.h>
void echo(char *str)
{
printf("%s",str);
}
int main(void)
{
char uname[32] = "sa";
char upass[32] = "thisispasswordarea";
char server[32] = "222.222.222.222:1678";
dbinit();
LOGINREC *loginrec = dblogin();
DBSETLUSER(loginrec,uname);
DBSETLPWD(loginrec,upass);
DBPROCESS *dbprocess = dbopen(loginrec,server);
echo("connecting...");
if(dbprocess == FAIL)
{
echo("connect...fail");
return 0;
}
if(dbuse(dbprocess,"db_6930") == FAIL)
{
echo("database select fail");
return 0;
}
else
{
echo("connect success.");
dbcmd(dbprocess,"select top 10 userid from wells.dbtest.dbo.user0");
if(dbsqlexec(dbprocess) == FAIL){
echo("query fail...");
return 0;
}
DBINT result_code;
char uid[20];
char nickname[100];
char email[200];
int rows = 0;
while((result_code = dbresults(dbprocess))!= NO_MORE_RESULTS)
{
if(result_code == SUCCEED)
{
dbbind(dbprocess,1,CHARBIND,(DBINT)0,(BYTE*)uid);
while(dbnextrow(dbprocess) != NO_MORE_ROWS)
{
rows++;
echo(uid);
}
}
}
printf("final row=%d",rows);
}
dbclose(dbprocess);
echo("connect closed.");
return 0;

}
存为emailParser.c
编译命令==============================
gcc emailParser.c -L/usr/local/freetds/lib -lsybdb
执行=========================
./a.out
输出================================
connecting...
connect success.
358568
358569
358570
358571
358572
358573
358574
358575
358576
358577
final row=10
connect closed.
==================
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐