(1) Installing freeTDS
FreeTDS provides an open source client of TDS protocol for Linux system. Because MS SQL and Sybase use the TDS protocol, they can connect MS SQL with FreeTDS in Linux.Official website: http://www.freetds.org
Download: wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
Installation:
[root@vm01 ~]# tar -zxvf 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
Modify environment variables:
FREETDS_HOME=/usr/local/freetds
export PATH=$FREETDS_HOME/bin:$PATH
Library file loading:
[root@vm01 freetds-0.91]# vim /etc/ld.so.conf.d/freetds.conf
/usr/local/freetds/lib
[root@vm01 freetds-0.91]# ldconfig
(2) Modify the configuration file and connect to the database
(i) Adding data sources
[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
After completion, you can connect to SQL Server 2005 using the following commands.
[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 [using test database]
2> go
1 > select * from StuInfo [Query Table Information]
2> go
StuID Name Age
001 zyh 24
002 zkl 21
003 Jim 24
(3 rows affected)
(ii) Modify protocol version to connect to SQL Server 2005 normally
Modify freetds configuration file
[root@vm01 test]# vim /usr/local/freetds/etc/freetds.conf
[global]
# TDS protocol version
#; tds version = 4.2
tds version=8.0 (there can be no comma before this)
Connecting to SQL SERVER 2005 requires a protocol version of 8.0, while using 4.2, the connection will fail. When using the tsql command to connect, if you do not configure the data source as in step (2), you also need to modify the protocol before you can normally connect to the database using the following commands:[root@vm01 test]# tsql -H 192.168.232.133 -p 1433 -U sa -P zkl
Note: The first p is lowercase and the last P is capitalized.
(3) Connecting database with C++ code
First, make sure that the protocol version is 8.0, then compile the following C code to test the connection to the database.
test.c:
-----------------------------------------------
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #Include <sybfront.h>//freetds header file
- #include <sybdb.h> //freetds
- int main(void)
- {
- char szUsername[32] = "sa";
- char szPassword[32] = "zkl";
- char szDBName[32] = "test"; //Database name
- char szServer[32] = "192.168.232.133:1433";//Database Server: Port
- //Initialize db-library
- dbinit();
- //Connect to the database
- LOGINREC *loginrec = dblogin();
- DBSETLUSER(loginrec, szUsername);
- DBSETLPWD(loginrec, szPassword);
- DBPROCESS *dbprocess = dbopen(loginrec, szServer);//Connect to the database
- 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");
- //query data base
- printf("[Query database tables]\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("[Insert data into database tables]\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("[Delete records from database tables]\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");
- //Close database connection
- dbclose(dbprocess);
- return 0;
- }