MingW下安装FreeTDS:
$ tar -xvzf /c/mingw/download/freetds-stable.tgz
$ cd freetds-0.91
$ ./configure --disable-libiconv --with-tdsver=7.1 --enable-msdblib --disable-shared --enable-static
$ make
$ make install
注意:
-with-tdsver=7.1中的7.1是目前最大版本,不能设置为8.0、9.0之类,否则将不能连接到SQL Server。比如,我设置为9.0,连接SQL Server 2008 R2就失败了。
MingW下测试FreeTDS代码:
- #include <stdio.h>
- #include <sybdb.h>
- // 编译:
- // gcc test.c -o testdb -L/local/lib/ -I/local/include -lsybdb -lws2_32
- int main(int argc, char **argv)
- {
- //初始化db库
- dbinit();
- //设置数据库信息
- LOGINREC *loginrec = dblogin();
- DBSETLUSER(loginrec, "user");
- DBSETLPWD(loginrec, "password*");
- // 设置UTF-8编码
- DBSETLCHARSET(loginrec, "UTF-8");
- //连接数据库
- DBPROCESS *dbprocess = dbopen(loginrec, "192.168.0.123:1433");
- if(dbprocess == FAIL)
- {
- printf("Connect fail\n");
- return 0;
- }
- printf("Connect success\n");
- //打开test数据库
- if(dbuse(dbprocess, "test") == FAIL)
- {
- printf("Open database fail\n");
- }
- else
- {
- printf("Open database success\n");
- }
- //查询test表
- dbcmd(dbprocess, "select id, name from test");
- if(dbsqlexec(dbprocess) == FAIL)
- {
- printf("Query table error\n");
- }
- DBINT result_code;
- char id[1024]={0};
- char value[1024]={0};
- while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS){
- if (result_code == SUCCEED){
- dbbind(dbprocess, 1, CHARBIND, (DBINT)0, (BYTE*)id);
- dbbind(dbprocess, 2, CHARBIND, (DBCHAR)0, (BYTE*)value);
- while (dbnextrow(dbprocess) != NO_MORE_ROWS){
- printf("id=%s\n", id);
- printf("value=%s\n", value);
- }
- }
- }
- //关闭数据库连接
- dbclose(dbprocess);
- return 0;
- }
本文转自 h2appy 51CTO博客,原文链接:http://blog.51cto.com/h2appy/994653,如需转载请自行联系原作者