Use the freetds database to connect to ms SQL Server

Source: Internet
Author: User
Tags sybase

1 OverviewConnecting to MSSQL in Linux is very painful because Microsoft did not provide any interfaces to developers. Fortunately, MSSQL is derived from Sybase, freetds is the underlying driver of the database. It is very convenient to use the freetds library for development. Freetds can connect to Sybase and ms SQL Server databases. 2 install, configure, and testOfficial site: http://www.freetds.org version: 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 Description: H: database iP P: Database port U: User Name P: Password 3 C/C ++ development in Linux 3.1 Use freetds's own program for developmentLocation in freetds-0.64/src/apps $ make clean $ make 3.2 rapid development using freetds LibraryTestsybase. 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"; // initialize DB-library dbinit (); // connect to the database 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 ");} // query the database 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) ;}}// close the database connection dbclose (dbprocess); Return 0 ;}compile: gcc-O testsybase. c. /src/dblib /. libs/libsybdb. A
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.