使用freetds庫串連MS SQL SERVER

來源:互聯網
上載者:User
  

1       概述在linux下串連MSSQL是一件很痛苦的事,因為微軟同志沒有提供任何介面給開發人員,還好,MSSQL是從Sybase衍生出來的,FreeTDS是一個資料庫底層的驅動程式,用freetds的庫進行開發是一件很方便的事。freetds可以串連sybase和ms sql server資料庫。 2       安裝,配置,測試官方網站:http://www.freetds.org版本:0.64$ tar -zxvf freetds-stable.tgz$ ./configure --prefix=/usr/local/freetds$ make$ su root$ make install$ tsql -H 2.2.2.2 -p 1433 -U test -P testtsql說明:    H:資料庫IP    p:資料庫連接埠    U:使用者名稱    P:密碼 3       linux下C/C++開發 3.1    利用freetds內建的程式開發位置在freetds-0.64/src/apps$make clean$make 3.2    利用freetds庫快速開發testsybase.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] = "test";       char szPassword[32] = "test";       char szDBName[32] = "test";       char szServer[32] = "2.2.2.2: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       /n");              return 0;       }else{              printf("ASB>>      ConnectEMS conect MS SQL SERVER success/n");       }       if(dbuse(dbprocess, szDBName) == FAIL){              printf("ASB>>      Open database name fail/n");       }else{              printf("ASB>>      Open database name success/n");       }              //查詢資料庫       dbcmd(dbprocess, "select ID,BeginTime,Description from Alarms");       if(dbsqlexec(dbprocess) == FAIL){              printf("ASB>>      Query Alarms table error/n");            }              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/n", szID);                            printf("ASB>>             szAid=%s/n", szBeginTime);                            printf("ASB>>             szBeginTime=%s/n", szDescription);                     }              }       }       //關閉資料庫連接       dbclose(dbprocess);              return 0;} 編譯: gcc -o testsybase testsybase.c ./src/dblib/.libs/libsybdb.a  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.