buggins / ddbc

DDBC is DB Connector for D language (similar to JDBC)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`trusted_connection` and `ODBC Driver 17 for SQL Server`

nlhnt opened this issue · comments

Hey, I am new to D-lang, any idea how to add trusted_connection=yes and ODBC Driver 17 for SQL Server to a connection string?
Is this supported or does this require a new PR?

example connection strings for the various databases are in the readme file. It looks like you should be doing something like:

ddbc:sqlserver://localhost,1433?user=sa,password=MyPassw0rd,driver=ODBC Driver 17 for SQL Server,trusted_connection=yes

I use "ODBC Driver 17 for SQL Server" for integration tests but that's done via an arg. I'm not sure if there'll be any problems with the spaces when setting it within the connection string. You may need to add " around it. Also, I don't recall having any tests to see if configuring trusted_connection is supported. If you encounter any problems please feedback with as much detail as possible.

I see, I tried doing this two days ago and it didn't work.
I am going to try again tomorrow and post some feedback, right now I am too physically tired to do just that... thanks for the response though! 👍🏻

Can't seem to get it to work.
Even with SA as user and password passed.
From the error log I would guess, that DDBC knows I want to select ODBC Driver 17 for SQL Server, but it just can't find the SQL Server instance.
Below is a piece of code I am trying to run.
The server is working ok, I have it running in a docker container and am able to connect to it using dbeaver (jdbc).

import std.stdio;
import ddbc;
import std.stdio;
import std.conv;

int main(string[] args) {

    // provide URL for proper type of DB
    string url = "ddbc:sqlserver://localhost,1433?user=sa,password=mysecretpassword,driver=ODBC Driver 17 for SQL Server"; // ,ssl=true
    // string url = "ddbc:odbc://?dsn=allocation/LPP_ForecastEcommerce?Trusted_Connection=yes,driver=ODBC Driver 17 for SQL Server"; // ,ssl=true
	// https://github.com/buggins/ddbc/blob/master/examples/odbc_test/source/main.d

    immutable string driverName = extractDriverNameFromURL(url);
    writeln("driverName:");
    writeln(driverName);

    // creating Connection
    //auto conn = ds.getConnection();
    Connection conn = createConnection(url);
    scope (exit)
        conn.close();

    // creating Statement
    auto stmt = conn.createStatement();
    scope (exit)
        stmt.close();

    import std.conv : to;
    auto rs = stmt.executeQuery("SELECT * FROM tempdb.dbo.TestTable");

    while (rs.next())
    {
        writeln(rs.getVariant(1), "\t", rs.getVariant(2), "\t", rs.getString(3), "\t", rs.getVariant(4));
    }

    return 0;
}

The error I am getting:

2022-04-15T12:52:09.019 [info] C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d:433:this Driver=ODBC Driver 17 for SQL Server;Server=er:\\localhost,1433;Uid=sa;Pwd=mysecretpassword
2022-04-15T12:52:14.111 [error] C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d:116:check odbc.sqlext.SQLDriverConnect(295162E6C70, null, 29517A90360, 85, CF96CFF100, 1024, CF96CFF502, 0) : SQL_ERROR

object.Exception@C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(437): 
        08001:1:11001   [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider:  Unknown host.

        HYT00:2:0       [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
        08001:3:11001   [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
----------------
0x00007FF7A3E44712 in ddbc.drivers.odbcddbc.extractError at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(157)
0x00007FF7A3E29310 in ddbc.drivers.odbcddbc.check!(SQLDriverConnect, "C:\\Users\\booko\\AppData\\Local\\dub\\packages\\ddbc-0.5.4\\ddbc\\source\\ddbc\\drivers\\odbcddbc.d", 437LU).check at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(125)
0x00007FF7A3E037D4 in ddbc.drivers.odbcddbc.ODBCConnection.checkdbc!(SQLDriverConnect, "C:\\Users\\booko\\AppData\\Local\\dub\\packages\\ddbc-0.5.4\\ddbc\\source\\ddbc\\drivers\\odbcddbc.d", 437LU).checkdbc at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(382)
0x00007FF7A3DEA30A in ddbc.drivers.odbcddbc.ODBCConnection.this at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(444)
0x00007FF7A3DE9924 in ddbc.drivers.odbcddbc.ODBCDriver.connect at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\drivers\odbcddbc.d(1709)
0x00007FF7A3DC5EE3 in ddbc.common.createConnection at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\common.d(589)
0x00007FF7A3DA4500 in D main at C:\Users\booko\AppData\Local\dub\packages\ddbc-0.5.4\ddbc\source\ddbc\common.d(587)
0x00007FF7A3FE9A93 in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll().__lambda2()
0x00007FF7A3FE98AF in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate())
0x00007FF7A3FE99BB in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll()
0x00007FF7A3FE98AF in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate())
0x00007FF7A3FE96B6 in d_run_main2
0x00007FF7A3FC8579 in d_run_main
0x00007FF7A3DA49A2 in app._d_cmain!().main at C:\Users\booko\scoop\apps\dmd\current\windows\bin64\..\..\src\druntime\import\core\internal\entrypoint.d(29)
0x00007FF7A40BA1AC in __scrt_common_main_seh at d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288)
0x00007FFC588E7034 in BaseThreadInitThunk
0x00007FFC5A862651 in RtlUserThreadStart
Program exited with code 1

That suggests you can't connect to SQL Server on localhost regardless of ddbc. Perhaps worth trying loopback address instead. Also is the port exposed? I'd test connectivity with telnet just to be sure

Hi @nlhnt sorry for the long delay in looking at this. I've just started looking at the mssql support in ddbc and found that the way additional parameters are handled prevents TrustServerCertificate=yes; from being added to the underlying connection properties.

It just needs an adjustment to the code in odbcddbc.d: https://github.com/buggins/ddbc/blob/master/source/ddbc/drivers/odbcddbc.d#L405-L423

I'll fix it this evening and release a new version

0.5.6 is available on https://code.dlang.org/packages/ddbc now