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

Connection to remote SQL Server using Qt



  • Hello,

    I am trying to connect to a remote SQL Server but I have got the following error message :

    [Microsoft] [SQL Server Native Client 11.0] TCP Provider: A connection attempt failed because the connected party did not respond properly beyond a certain time or a connection did not respond.

    [Microsoft] [SQL Server Native Client 11.0] A network related or instance specific error occurred while establishing a connection to SQL Server. The server cannot be found or is not accessible. Check if the instance name is correct and if SQL Server is configured to allow remote connections. For more information, see SQL Server Books Online. [Microsoft] [SQL Server Native Client 11.0] Connection timeout expired QODBC3: Unable to connect

    I checked :

    • Firewall input/output rules
    • SQL Server is allowing distant connection
    • I can run a remote SQL query with SQL Server Management Studio 18 (server name = 192.168.0.15, port = 1433, user = user_uid, password = password)

    My current code is :

    QString connectString = "Driver={SQL Server Native Client 11.0};";
    connectString.append("Server=169.168.0.15,1433;"); // IP,Port
    connectString.append("Database=remote_database;");  // Schema
    connectString.append("Uid=user_uid;");           // User
    connectString.append("Pwd=password;");           // Pass
    myDataBase.setDatabaseName(connectString);
    

    Before that my code worked with a local SQL Server :

    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=lpc:MYCOMPUTER\\SQLEXPRESS;Database=local_database;Trusted_Connection=Yes;"));
    myDataBase.setUserName("user_uid");
    myDataBase.setPassword("password");
    

    I tested different options, but I always get the same message

    // With or without SQLEXPRESS
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=169.168.0.15\\SQLEXPRESS,1433;Database=remote_database;Trusted_Connection=Yes;"));
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=169.168.0.15,1433;Database=remote_database;Trusted_Connection=Yes;"));
    // With (below) or without (above) tcp:
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=tcp:169.168.0.15\\SQLEXPRESS,1433;Database=remote_database;Trusted_Connection=Yes;"));
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=tcp:169.168.0.15,1433;Database=remote_database;Trusted_Connection=Yes;"));
    //With (above) or without (below) port
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=tcp:169.168.0.15\\SQLEXPRESS;Database=remote_database;Trusted_Connection=Yes;"));
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=tcp:169.168.0.15;Database=remote_database;Trusted_Connection=Yes;"));
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=169.168.0.15\\SQLEXPRESS;Database=remote_database;Trusted_Connection=Yes;"));
    myDataBase.setDatabaseName(QString("DRIVER={SQL Server Native Client 11.0};SERVER=169.168.0.15;Database=remote_database;Trusted_Connection=Yes;"));
    

    I am using :

    • QT 5.15.1
    • Qt Creator 4.12.4
    • mingw32

    Any ideas ?



  • @Touchoco said in Connection to remote SQL Server using Qt:

    I can run a remote SQL query with SQL Server Management Studio 18 (server name = 192.168.0.15, port = 1433, user = user_uid, password = password)

    but everywhere else you have SERVER=169.168.0.15?


  • Lifetime Qt Champion

    Don't see anything Qt-related here what could go wrong - Qt does not change the connection strings but simply pass them to the driver. The ODBC connection strings are a mess. Maybe use another sql browser where you can enter the sql conection strings directly to fiddle out what's the correct one.



  • @Touchoco said in Connection to remote SQL Server using Qt:

    I can run a remote SQL query with SQL Server Management Studio 18 (server name = 192.168.0.15, port = 1433, user = user_uid, password = password)

    but everywhere else you have SERVER=169.168.0.15?



  • @artwaw

    Thanks a lot ! It works.

    I don't know how I could miss this one. It seems that every time I restart the server, the IP changes. I will have to set a fixed IP adress.