FreeTDS实现在unix- Linux&nb…

本文详细介绍了如何在Unix/Linux环境下安装FreeTDS,配置并测试与MSSQLSERVER2000的连接,最终通过C/C++编程实现数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  一、 安装FreeTDS
  1、 到官方网站http://www.freetds.org/下freetds-stable.tgz(最新为0.64)
  2、 用root登陆unix/linux
  3、 #mkdir /mypkg
  4、 #mv freetds-stable.tgz /mypkg/freetds-stable.tgz
  5、 #cd /mypkg
  6、 #tar zxvf freetds-stable.tgz
  7、 #cd freetds-0.91
  8、 # ./configure --prefix=/opt/local/freetds --enable-msdblib --with-unixodbc=/usr --with-tdsver=8.0

  9、 #make #make install
  10、 修改/opt/local/freetds/etc/freetds.conf
  将其中的
  ;[egServer70]
  ; host = ntmachine.domain.com
  ; port = 1433
  ; tds version = 8.0
  改为 
  # A typical Microsoft server
    [egServer70]
host = 192.168.0.177
port = 1433
tds version = 7.1

  11、 设置环境变量(bash)
  /etc/profile加上下面的内容后退出
  
#------------------------------------------------------------------------
# Setup freetds (for MS SQL SERVER 2k) environment 
#------------------------------------------------------------------------
SYBASE=/opt/local/freetds export SYBASE
export TDSVER=7.1
export PATH=$PATH:$SYBASE/bin  
export MANPATH=$MANPATH:$SYBASE/share/man
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$SYBASE/lib 
 

  二、 测试tsql
  在测试我们先在服务器端做一点小的操作
  打开企业管理器,新建用户guest,以sql servre登陆的认证方式,默认数据库为pubs,密码为:123456
  用普通用户登陆到unix/linux
  $ tsql  -S egServer70 -U guest -P123456
  1> select * from authors
  2> go
  ……

三、 用DB-Library实现在C/C++中访问MS SQL SERVER2000
  $mkdir tdsprog
  $cd tdsprog
  在tdsprog目录下建立以下三个文件
  1、 config.mk
  
  # Edit the following for your installation
  
  CC = gcc
  FREETDSINC = /opt/local/freetds/include
  FREETDSLIB = /opt/local/freetds/lib
  LIBS = -lsybdb -lm -lpthread 
  
  #==============================================================
  # Compiler and linker flags
  
  CFLAGS = -O -I$(FREETDSINC)
  LFLAGS = -O -L$(FREETDSLIB)
  
  2、 Makefile
  
  #==============================================================
  CONFIG = ./config.mk
  include $(CONFIG)
  .SUFFIXES: .c
  .c.o:
   $(CC) $(CFLAGS) -c $<</div>
  #==============================================================
  # This program's object code files
  
  OBJS = 1.o
  
  #==============================================================
  # File dependencies and rules
  
  all: 1
  
  objs: $(OBJS)
  
  clean:
   rm -f $(OBJS)
   rm -f *~
   rm -f 1
  
  1: $(OBJS)
   $(CC) -o $@ $(LFLAGS) $(OBJS) $(LIBS)
  
  1.o: 1.c
  
  3、 1.c(源程序)
  
#include
#include
#include
#include
#include

int main(int argc,char argv) {
LOGINREC *Ldblogin;
DBPROCESS *dbproc;
int iRetCode=0;
RETCODE return_code,n_ret_code;
char cValue[120];
DBINT iColLength=0;
DBINT iRet;
DBINT result_code;
int i=0;
memset(cValue, 0, sizeof(cValue));
if(dbinit()==FAIL)
exit(-1);
Ldblogin = dblogin();
DBSETLUSER(Ldblogin, "guest");
DBSETLPWD(Ldblogin, "123456");
DBSETLAPP(Ldblogin, "myprog");
dbproc = dbopen(Ldblogin, "egServer70");
dbcmd(dbproc, "select au_lname from authors");
dbsqlexec(dbproc);

while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) {
if (result_code == SUCCEED) {
memset(cValue, 0, sizeof cValue);
dbbind(dbproc, 1, CHARBIND, (DBINT)0, (BYTE *) cValue);
while (dbnextrow(dbproc) != NO_MORE_ROWS){
i++;
iColLength = dbdatlen(dbproc, 1);
cValue [iColLength]='\0';
printf("\nauthor name=%s\n", cValue);
}
}
}
return 0;
}

  
  编译并运行
  $make
  $./1
  运行结果如下:
  author name=Bennet
  author name=Blotchet-Halls
  author name=Carson
  author name=DeFrance
  author name=del Castillo
  author name=Dull
  author name=Green
  author name=Greene
  author name=Gringlesby
  author name=Hunter
  author name=Karsen
  author name=Locksley
  author name=MacFeather
  author name=McBadden
  author name=O'Leary
  author name=Panteley
  author name=Ringer
  author name=Ringer
  author name=Smith
  author name=Straight
  author name=Stringer
  author name=White
  author name=Yokomoto

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值