Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)

(1)安装freeTDS

FreeTDS为Linux系统提供了TDS协议的开源客户端。由于MS SQL和Sybase使用的恰是TDS协议,所以在Linux中可以用FreeTDS连接MS SQL。

官网:http://www.freetds.org

下载:wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz

安装:
[root@vm01 ~]# tar zxf freetds-stable.tgz
[root@vm01 ~]# cd freetds-0.91
[root@vm01 freetds-0.91]# ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib
[root@vm01 freetds-0.91]# make
[root@vm01 freetds-0.91]# make install

修改环境变量:
FREETDS_HOME=/usr/local/freetds
export PATH=$FREETDS_HOME/bin:$PATH

库文件加载:
[root@vm01 freetds-0.91]# vim /etc/ld.so.conf.d/freetds.conf
/usr/local/freetds/lib
[root@vm01 freetds-0.91]# ldconfig

(2) 修改配置文件,连接数据库
(i) 添加数据源
[root@vm01 test]# vim /usr/local/freetds/etc/freetds.conf
#zkl add
[SQL2005]
        host = 192.168.232.133
        port = 1433
        tds version=8.0
#client charset = ISO-8859-1
完成后,使用如下命令即可连接到 SQL Server 2005 .
[root@vm01 test]# tsql -S SQL2005 -U sa -P zkl
locale is "zh_CN.GB18030"
locale charset is "GB18030"
using default charset "GB18030"
1> use test  [使用test数据库]
2> go
1> select * from StuInfo [查询表信息]
2> go
StuID Name Age
001 zyh 24
002 zkl 21
003 Jim 24
(3 rows affected)

(ii) 修改协议版本以正常连接SQL Server 2005
修改 freetds 配置文件
[root@vm01 test]# vim /usr/local/freetds/etc/freetds.conf
[global]
        # TDS protocol version
#;      tds version = 4.2

         tds version=8.0

连接 SQL SERVER 2005 需要使用的协议版本为 8.0,而使用 4.2 时,连接将会失败。使用 tsql 命令连接时,如果不像步骤(2)中那样配置数据源,则同样需要修改协议,然后才能使用如下命令正常连接数据库:
[root@vm01 test]# tsql -H 192.168.232.133 -p 1433 -U sa -P zkl

注意:第一个p为小写,后面的P是大写。

(3) 使用C++代码连接数据库
首先,确保协议版本是 8.0,然后编译以下C代码,测试与数据库的连接。

test.c:
-----------------------------------------------
  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <stdlib.h> 
  4. #include <unistd.h>  
  5.   
  6. #include <sybfront.h> //freetds头文件 
  7. #include <sybdb.h> //freetds 
  8.   
  9.   
  10. int main(void
  11.     char szUsername[32] = "sa"
  12.     char szPassword[32] = "zkl"
  13.     char szDBName[32] = "test"; //数据库名 
  14.     char szServer[32] = "192.168.232.133:1433";//数据库服务器:端口 
  15.   
  16.     //初始化db-library 
  17.     dbinit(); 
  18.         
  19.     //连接数据库 
  20.     LOGINREC *loginrec = dblogin(); 
  21.     DBSETLUSER(loginrec, szUsername);        
  22.     DBSETLPWD(loginrec, szPassword); 
  23.     DBPROCESS *dbprocess = dbopen(loginrec, szServer);//连接数据库 
  24.     if(dbprocess == FAIL) 
  25.     { 
  26.         printf("Conect to MS SQL SERVER fail, exit!\n"); 
  27.         return -1;  
  28.     } 
  29.     printf("Connect to MS SQL SERVER success!\n"); 
  30.         
  31.     if(dbuse(dbprocess, szDBName) == FAIL) 
  32.         printf("Open database failed!\n"); 
  33.     else 
  34.         printf("Open database success!\n"); 
  35.         
  36.     //查询数据库 
  37.     printf("[查询数据库表]\n"); 
  38.     dbcmd(dbprocess, "select StuID, Name, Age from StuInfo"); 
  39.     if(dbsqlexec(dbprocess) == FAIL) 
  40.     { 
  41.         printf("Query table 'StuInfo' error.\n"); 
  42.         return -1;  
  43.     } 
  44.       
  45.     DBINT result_code; 
  46.     char szStuID[20]={}; 
  47.     char szName[80]={}; 
  48.     char szAge[10]={}; 
  49.     int rows = 0; 
  50.     while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS){ 
  51.         if (result_code == SUCCEED){ 
  52.             dbbind(dbprocess, 1, CHARBIND, (DBINT)0, (BYTE*)szStuID); 
  53.             dbbind(dbprocess, 2, CHARBIND, (DBCHAR)0, (BYTE*)szName); 
  54.             dbbind(dbprocess, 3, CHARBIND, (DBCHAR)0, (BYTE*)szAge); 
  55.             printf("StuID\tName\tAge\n", szStuID); 
  56.             while (dbnextrow(dbprocess) != NO_MORE_ROWS){                         
  57.                 printf("%s\t", szStuID); 
  58.                 printf("%s\t", szName); 
  59.                 printf("%s\n", szAge); 
  60.             } 
  61.         } 
  62.     }        
  63.   
  64.     printf("[插入数据到数据库表]\n"); 
  65.     dbcmd(dbprocess, "insert into StuInfo(StuID, Name, Age) values(888,'James',28)"); 
  66.     if(dbsqlexec(dbprocess) == FAIL) 
  67.     { 
  68.         printf("insert into table 'StuInfo' error.\n"); 
  69.         return -1;  
  70.     } 
  71.     printf("insert into table 'StuInfo' success.\n"); 
  72.      
  73.     printf("[删除数据库表中的记录]\n"); 
  74.     dbcmd(dbprocess, "delete from StuInfo where StuID=888"); 
  75.     if(dbsqlexec(dbprocess) == FAIL) 
  76.     { 
  77.         printf("delete from table 'StuInfo' error.\n"); 
  78.         return -1;  
  79.     } 
  80.     printf("delete from table 'StuInfo' success.\n"); 
  81.      
  82.     //关闭数据库连接 
  83.     dbclose(dbprocess); 
  84.  
  85.     return 0; 
  86. }  


-----------------------------------------------

Makefile:
-----------------------------------------------
default:
gcc test.c -o test -L/usr/local/freetds/lib -lsybdb -I/usr/local/freetds/include
-----------------------------------------------
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:25 天前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐