Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlDatabase ODBC and timeout



  • Hello,
    i'm trying to change the connect timeout of my OBDC sql.

        QSqlDatabase lSqlDatabase = QSqlDatabase::addDatabase( "QODBC", "databasename" );
        lSqlDatabase.setDatabaseName("DRIVER=SQL Server;SERVER=servername;DATABASE=databasename");
        lSqlDatabase.setConnectOptions( "SQL_ATTR_CONNECTION_TIMEOUT=2;SQL_ATTR_LOGIN_TIMEOUT=2" );
        auto a = lSqlDatabase.connectOptions();
    
        lSqlDatabase.open(); //Really slow
    

    The timeout doesnt change, and when I try to get the connect options, I get nothing. LastError returns nothing aswell.

    Any idea on how to actually change the timeout?



  • have you tried using one of the many ODBC DSN connection testing utilities out there to verify that the DSN is valid in the first place? Also, google indicates that you can supply connection options as part of the DSN connection string.



  • @Mwoua said in QSqlDatabase ODBC and timeout:

        QSqlDatabase lSqlDatabase = QSqlDatabase::addDatabase( "QODBC", "databasename" );
        lSqlDatabase.setDatabaseName(DRIVER=SQL Server;SERVER=servername;DATABASE=databasename);
        lSqlDatabase.setConnectOptions( "SQL_ATTR_CONNECTION_TIMEOUT=2;SQL_ATTR_LOGIN_TIMEOUT=2" );
        auto a = lSqlDatabase.connectOptions();
    
        lSqlDatabase.open(); //Really slow
    

    Is this a typo? Your connection string Driver attribute should look like: Driver={SQL Server}



  • @Mwoua
    Further to @sneubert , could you explain how the line you have pasted from your code:

    lSqlDatabase.setDatabaseName(DRIVER=SQL Server;SERVER=servername;DATABASE=databasename);

    gets through your compiler, given that it's not valid in C++?



  • Sorry for the delay.

    @Kent-Dorfman It's valid, im trying to change the timeout when the user isnt connected to our VPN and has no access to the databse. When the user is on our network, it works correctly.
    I didnt find anything to set the timeout in the connection string, do you have an example?

    @JonB Its a variable in the code, I forgot to add the quote when i replaced it. First post edited

    @sneubert the setDatabaseName argument doesnt have any {} and it seems to work when the database is reachable

    EDIT: I found these two links
    https://docs.microsoft.com/en-us/sql/relational-databases/native-client-odbc-api/sqlsetconnectattr?view=sql-server-2017
    https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetconnectattr-function?view=sql-server-2017

    and the first one says that SQL_ATTR_CONNECTION_TIMEOUT is ignored. Maybe thats the problem


Log in to reply