linux下使用Freetds 连接MS SqlServer 2000 (C语言实现)

来源:互联网 发布:查询没有记录的 数据库 编辑:程序博客网 时间:2024/05/16 17:41

1.前提条件

  freetds开发包: 到(http://www.freetds.org)下载当前版本0.64

2. 安装

 解压:  tar -zxvf freetds-stable.tgz

         ./configure

         make

         make install

3.测试。

      1.使用TSQL

         tsql -H 1.1.1.1 -p 1433 -U test -P test

     2.使用C语言

#include <stdio.h> 
#include 
<string.h> 
#include 
<stdlib.h> 
#include 
<unistd.h>  
  
#include 
<sybfront.h> 
#include 
<sybdb.h> 
  
  
  
int main(void

       
char szUsername[32= "sa"
       
char szPassword[32= "123456"
       
char szDBName[32= "oraldb"
       
char szServer[32= "192.168.1.200:1433"
  
       
//初始化db-library 
       dbinit(); 
       
//
       LOGINREC *loginrec = dblogin(); 
       DBSETLUSER(loginrec, szUsername);        
       DBSETLPWD(loginrec, szPassword); 
       DBPROCESS 
*dbprocess = dbopen(loginrec, szServer); 
       
if(dbprocess == FAIL)
              printf(
"ASB>>      Conect MS SQL SERVER fail        "); 
              
return 0
}
else
              printf(
"ASB>>      ConnectEMS conect MS SQL SERVER success "); 
       }
 
       
if(dbuse(dbprocess, szDBName) == FAIL)
              printf(
"ASB>>      Open database name fail "); 
       }
else
              printf(
"ASB>>      Open database name success "); 
       }
 
        
       
//执行查询
         dbcmd(dbprocess, "select type ,stuid,stuname from oral_stuinfo"); 
       
if(dbsqlexec(dbprocess) == FAIL)
              printf(
"ASB>>      Query Alarms table error ");      
       }
 
        
       DBINT result_code; 
       
char szID[1024]; 
       
char szBeginTime[1024]; 
       
char szDescription[1024]; 
       
int rows = 0
       
while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS)
              
if (result_code == SUCCEED)
                     dbbind(dbprocess, 
1, CHARBIND, (DBINT)0, (BYTE*)szID); 
                     dbbind(dbprocess, 
2, CHARBIND, (DBCHAR)0, (BYTE*)szBeginTime); 
                     dbbind(dbprocess, 
3, CHARBIND, (DBCHAR)0, (BYTE*)szDescription); 

   
while (dbnextrow(dbprocess) != NO_MORE_ROWS){                         
                            printf(
"ASB>>             ID=%s ", szID); 
                            printf(
"ASB>>             szAid=%s ", szBeginTime); 
                            printf(
"ASB>>             szBeginTime=%s ", szDescription); 
                     }
 
              }
 
       }
 
       
//操作结束关闭对象
       dbclose(dbprocess); 
        
       
return 0
}
 

 

tsql说明:
    H:数据库IP
    p:数据库端口
    U:用户名
    P:密码
原创粉丝点击