linux 下用freetds 鏈接sqlserver
(1)安裝freeTDS sql
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
歡迎關注本站公眾號,獲取更多信息