当前位置: 首页 > 工具软件 > FreeTDS > 使用案例 >

Linux下C语言通过Freetds连接SQLServer

柳俊彦
2023-12-01
  1. 在Linux使用C语言访问SQLServer时需要通过Freetds驱动去连接

  2. Freetds安装点击跳转到另一篇文章

  3. 按照上述文章,安装好驱动后,测试demo如下:(SQLServer已安装)

    #include<stdio.h>  
    #include<string.h>  
    #include<stdlib.h>  
    #include<unistd.h> 
    #include <time.h>
    #include <sys/time.h> 
    #include<sybfront.h>  
    #include<sybdb.h>  
    //gcc -o demo demo.c -L /usr/local/lib/ -lsybdb -I /usr/local/include/
    
    char time_tmp[128] = "";
    char *display_time(void){
    	struct timeval tv;
    	struct timezone tz;
    	struct tm *tp;
    	gettimeofday(&tv, &tz);
    	tp=localtime(&tv.tv_sec);
    	memset(time_tmp, 0, sizeof(time_tmp));
    	sprintf(time_tmp, "%4d-%02d-%02d %02d:%02d:%02d.%03d",1900+tp->tm_year, 1+tp->tm_mon, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, (int)(tv.tv_usec/1000));
    	return time_tmp;
    }
    #define LOG(format, ...) do { printf("[%s]:[%s] " format "\n", display_time(), __FUNCTION__, ##__VA_ARGS__ );} while (0)
    
    void get_tb_User(DBPROCESS *dbprocess);
    
    int main(int argc, char* argv[]) {    
        char szUsername[32]= "sa";  
        char szPassword[32]= "admin";  
        char szDBName[32]= "db_test";  
        char szServer[32]= "SQLTest"; 
        if(argc >= 2){
            strcpy(szUsername, argv[1]);
            LOG("Input szUsername:%s\n", argv[1]);
        }
        if(argc >= 3){
            strcpy(szPassword, argv[2]);
            LOG("Input szPassword:%s\n", argv[2]);
        }
        if(argc >= 4){
            strcpy(szDBName, argv[3]);
            LOG("Input szDBName:%s\n", argv[3]);
        }
        if(argc >= 5){
            strcpy(szServer, argv[4]);
            LOG("Input szServer:%s\n", argv[4]);
        }
        LOG("szUsername:%s, szPassword:%s, szDBName:%s, szServer:%s", szUsername, szPassword, szDBName, szServer);
        //初始化db-library  
        dbinit();
        //连接数据库  
        LOGINREC *loginrec=dblogin();  
        // 设置登录的用户名
        DBSETLUSER(loginrec,szUsername);  
        // 设置登录密码
        DBSETLPWD(loginrec,szPassword);  
    	
        // 连接sqlserver服务器地址和端口号,这里才是连接
        DBPROCESS *dbprocess=dbopen(loginrec,szServer);  
        if(dbprocess==FAIL){  
            LOG("Connect MSSQLSERVER fail\n");  
            return 0;  
        }else{  
            LOG("Connect MSSQLSERVER success\n");  
        }  
        // 连接数据库
        if(dbuse(dbprocess,szDBName)==FAIL){  
            LOG("Open data basename fail\n");  
        }else{  
            LOG("Open data basename success\n");  
        }  
        // 查询数据库中表中的内容
        while(1){
            get_tb_User(dbprocess);
            sleep(10);
        }
        //关闭数据库连接  
        dbclose(dbprocess);  
        return 0;  
    }
    
    void get_tb_User(DBPROCESS *dbprocess){
        LOG("Begin Read !");
        int tb_count = 0;
        dbcmd(dbprocess,"SELECT id as uid, account, sex,  name FROM tb_User");  //查询数据表中的内容,更具实际情况更改数据表名称 
        // 执行命令
        if(dbsqlexec(dbprocess)==FAIL){  
            LOG("Query table error\n");  
        }else{
            DBINT result_code = 0;   
            // 查看命令执行的结果。
            while((result_code=dbresults(dbprocess))!=NO_MORE_RESULTS)  {  
                DBINT uid = 0; 
                DBCHAR account[32] = "";  
                DBINT sex = 0;
                DBCHAR name[32] = "";
    
                if(result_code==SUCCEED){  
                    dbbind(dbprocess,1,INTBIND,(DBINT)0,(BYTE*)&uid);  
                    dbbind(dbprocess,2,CHARBIND,(DBCHAR)0,(BYTE*)account);  
                    dbbind(dbprocess,3,INTBIND,(DBINT)0,(BYTE*)&sex);    
                    dbbind(dbprocess,4,CHARBIND,(DBCHAR)0,(BYTE*)name); 
                    while(dbnextrow(dbprocess)!=NO_MORE_ROWS){  
                        tb_count++;
                        // 打印本行的数据
                        printf("uid:%d, account:%s, sex:%d, name:%s\n",uid, account, sex, name);  
                    }  
                }  
            }  
        }
        LOG("Read tb_count:%d", tb_count);
    }
    
    
  4. 编译:gcc -o demo demo.c -L /usr/local/lib/ -lsybdb -I /usr/local/include/

 类似资料: