Linux下连接Sql Server

首先,俺安装了 
freetds-bin【FreeTDS command-line utilities】
freetds-common【configuration files for FreeTDS SQL client libraries】
freetds-dev【MS SQL and Sybase client library (static libs and headers)】 
这三样东西,都是用apt-get安装的。

安装好了之后,首先去修改 Freetds 的配置文件,如果不知道在哪,就用 find / -name freetds 查找相关文件夹,在我的机器上查找到了两处:
/usr/share/freetds
/etc/freetds
然后我修改了  /etc/freetds 文件夹中的  freetds.conf 。

用 vim 打开,在下面加上:
[localhost]【下面这个配置的名字】
        host = 192.168.0.2【SQL Server所在的IP】
        port = 1433【或者自己设置的SQL Server端口】
        tds version = 8.0【打开SQL Server查看版本号,这个和那个对应】

这时候,就已经能够在命令行下直接调用了,命令是这样的: 
localhost:/etc/freetds# tsql -S localhost【你配置文件里取的名字】 -U 【数据库登录用户】 -P 【登录密码】
命令行显示:【1> 】就对了,可以在后面输入SQL语句,回车是换行,想要执行,则输入【go】之后回车。  

下面是简单的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. }  


代码写完之后,编译的时候需要链接正确才能通过,整句话是这样的:

gcc -o dbTest dbTest.c -lrt /usr/lib/libsybdb.a 【一定要加入 -lrt 才行

然后运行程序,看看是不是成功了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值