(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:
-----------------------------------------------
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sybfront.h> //freetds头文件
- #include <sybdb.h> //freetds
- int main(void)
- {
- char szUsername[32] = "sa";
- char szPassword[32] = "zkl";
- char szDBName[32] = "test"; //数据库名
- char szServer[32] = "192.168.232.133:1433";//数据库服务器:端口
- //初始化db-library
- dbinit();
- //连接数据库
- LOGINREC *loginrec = dblogin();
- DBSETLUSER(loginrec, szUsername);
- DBSETLPWD(loginrec, szPassword);
- DBPROCESS *dbprocess = dbopen(loginrec, szServer);//连接数据库
- if(dbprocess == FAIL)
- {
- printf("Conect to MS SQL SERVER fail, exit!\n");
- return -1;
- }
- printf("Connect to MS SQL SERVER success!\n");
- if(dbuse(dbprocess, szDBName) == FAIL)
- printf("Open database failed!\n");
- else
- printf("Open database success!\n");
- //查询数据库
- printf("[查询数据库表]\n");
- dbcmd(dbprocess, "select StuID, Name, Age from StuInfo");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("Query table 'StuInfo' error.\n");
- return -1;
- }
- DBINT result_code;
- char szStuID[20]={};
- char szName[80]={};
- char szAge[10]={};
- int rows = 0;
- while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS){
- if (result_code == SUCCEED){
- dbbind(dbprocess, 1, CHARBIND, (DBINT)0, (BYTE*)szStuID);
- dbbind(dbprocess, 2, CHARBIND, (DBCHAR)0, (BYTE*)szName);
- dbbind(dbprocess, 3, CHARBIND, (DBCHAR)0, (BYTE*)szAge);
- printf("StuID\tName\tAge\n", szStuID);
- while (dbnextrow(dbprocess) != NO_MORE_ROWS){
- printf("%s\t", szStuID);
- printf("%s\t", szName);
- printf("%s\n", szAge);
- }
- }
- }
- printf("[插入数据到数据库表]\n");
- dbcmd(dbprocess, "insert into StuInfo(StuID, Name, Age) values(888,'James',28)");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("insert into table 'StuInfo' error.\n");
- return -1;
- }
- printf("insert into table 'StuInfo' success.\n");
- printf("[删除数据库表中的记录]\n");
- dbcmd(dbprocess, "delete from StuInfo where StuID=888");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("delete from table 'StuInfo' error.\n");
- return -1;
- }
- printf("delete from table 'StuInfo' success.\n");
- //关闭数据库连接
- dbclose(dbprocess);
- return 0;
- }
[cpp]
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sybfront.h> //freetds头文件
- #include <sybdb.h> //freetds
- int main(void)
- {
- char szUsername[32] = "sa";
- char szPassword[32] = "zkl";
- char szDBName[32] = "test"; //数据库名
- char szServer[32] = "192.168.232.133:1433";//数据库服务器:端口
- //初始化db-library
- dbinit();
- //连接数据库
- LOGINREC *loginrec = dblogin();
- DBSETLUSER(loginrec, szUsername);
- DBSETLPWD(loginrec, szPassword);
- DBPROCESS *dbprocess = dbopen(loginrec, szServer);//连接数据库
- if(dbprocess == FAIL)
- {
- printf("Conect to MS SQL SERVER fail, exit!\n");
- return -1;
- }
- printf("Connect to MS SQL SERVER success!\n");
- if(dbuse(dbprocess, szDBName) == FAIL)
- printf("Open database failed!\n");
- else
- printf("Open database success!\n");
- //查询数据库
- printf("[查询数据库表]\n");
- dbcmd(dbprocess, "select StuID, Name, Age from StuInfo");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("Query table 'StuInfo' error.\n");
- return -1;
- }
- DBINT result_code;
- char szStuID[20]={};
- char szName[80]={};
- char szAge[10]={};
- int rows = 0;
- while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS){
- if (result_code == SUCCEED){
- dbbind(dbprocess, 1, CHARBIND, (DBINT)0, (BYTE*)szStuID);
- dbbind(dbprocess, 2, CHARBIND, (DBCHAR)0, (BYTE*)szName);
- dbbind(dbprocess, 3, CHARBIND, (DBCHAR)0, (BYTE*)szAge);
- printf("StuID\tName\tAge\n", szStuID);
- while (dbnextrow(dbprocess) != NO_MORE_ROWS){
- printf("%s\t", szStuID);
- printf("%s\t", szName);
- printf("%s\n", szAge);
- }
- }
- }
- printf("[插入数据到数据库表]\n");
- dbcmd(dbprocess, "insert into StuInfo(StuID, Name, Age) values(888,'James',28)");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("insert into table 'StuInfo' error.\n");
- return -1;
- }
- printf("insert into table 'StuInfo' success.\n");
- printf("[删除数据库表中的记录]\n");
- dbcmd(dbprocess, "delete from StuInfo where StuID=888");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("delete from table 'StuInfo' error.\n");
- return -1;
- }
- printf("delete from table 'StuInfo' success.\n");
- //关闭数据库连接
- dbclose(dbprocess);
- return 0;
- }
-----------------------------------------------
Makefile:
-----------------------------------------------
default:
gcc test.c -o test -L/usr/local/freetds/lib -lsybdb -I/usr/local/freetds/include