Connection string for connecting to SQL Server on Ubuntu
Consider this scenario.
-
SQL Server is running on ubuntu
-
Able to connect through Azure Data Studio
-
Able to connect through sqlcmd
sqlcmd -S 192.168.99.100,31433 -U sa -P S0mePassw0rd -d friends -Q "SELECT TOP 5 * FROM dbo.users;"
But I'm not able to connect through C code using ODBC from the local machine.
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
returns -1 and SQLAllocHandle
fails.
It's probably a faulty connection string but I've tried hundred different ones without success.
Latest is
Server=friends.laurijssen.local,31433;UID=sa;PWD=S0mePassw0rd;
What should be the correct string to connect to 192.168.100.99 (laurijssen.local) on port 31433 and database friends?
I would try
Server=laurijssen.local,31433;database=friends;UID=sa;PWD=S0mePassw0rd;
or
Server=192.168.100.99,31433;database=friends;UID=sa;PWD=S0mePassw0rd;
The server=
part should be just the server name (machine name) or IP address - plus port, if a custom port is used; the database should be specified separately with a database=
key
For reference, see https://www.connectionstrings.com/microsoft-sql-server-odbc-driver/