用于在Ubuntu上连接到SQL Server的连接字符串

问题描述

考虑这种情况。

  • sql Server在ubuntu上运行

  • 能够通过Azure Data Studio连接

  • 能够通过sqlcmd连接

    sqlcmd -S 192.168.99.100,31433 -U sa -P S0mePassw0rd -d friends 
           -Q "SELECT TOP 5 * FROM dbo.users;"
    

但是我无法使用本地计算机上的ODBC通过C代码进行连接。

retcode = sqlDriverConnect(hdbc,NULL,connectionstring,sql_NTS,outstr,sizeof(outstr),&outstrlen,sql_DRIVER_nopROMPT);

retcode = sqlAllocHandle(sql_HANDLE_STMT,hdbc,&hstmt);
CHECK_ERROR(retcode,"sqlAllocHandle(sql_HANDLE_STMT)",

sqlDriverConnect返回-1,而sqlAllocHandle失败。

可能是错误的连接字符串,但我尝试了数百种不同的连接字符串,但均未成功。

最新的是

Server=friends.laurijssen.local,31433;UID=sa;PWD=S0mePassw0rd;

在端口31433和数据库好友上连接到192.168.100.99(laurijssen.local)的正确字符串应该是什么?

解决方法

我会尝试

Server=laurijssen.local,31433;database=friends;UID=sa;PWD=S0mePassw0rd;

Server=192.168.100.99,31433;database=friends;UID=sa;PWD=S0mePassw0rd;

server=部分应该仅仅是服务器名称(机器名称)或IP地址-如果使用了自定义端口,则应加上端口;应该使用database=键单独指定数据库

有关参考,请参见https://www.connectionstrings.com/microsoft-sql-server-odbc-driver/