在linux下通过C语言基于freetds连接sql2000
来源:程序员人生 发布时间:2013-10-05 03:02:30 阅读次数:3977次
安装好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.
==================
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠