Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MS SQLServer from Qt Linux application

I'm trying to connect to a MS SQL Server on a remote box using QODBC in my Qt Linux application.

Here's what I have done so far:

  1. Added QT += SQL in the .pro file.

  2. Tested some db functions:

    QStringList drivers = QSqlDatabase::drivers();
    qDebug() << "Drivers: " ;
    
    foreach(QString driver, drivers) {
        qDebug() << ":: " << driver;
    }
    
    qDebug() << "Connection Names: ";
    QStringList connames = QSqlDatabase::connectionNames();
    
    foreach(QString conname, connames) {
        qDebug() << ":: " << conname;
    }
    qDebug() << "---";
    

these both work, though connectionNames() is empty at this stage.

  1. I have tried to added a database:

    QString serverName = "server1";
    QString dbName = "abc123";
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "MyFirst");
    db.setHostName(serverName);
    
    QString myCon = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection = Yes").arg(serverName).arg(dbName);
    
    db.setDatabaseName(myCon);
    

If I now list the connections, "MyFirst" is in the list.

  1. Tried to open the database:

    bool ok = db.open();    
    qDebug() << "OK: " << ok;
    
    if (!ok) {
        qDebug() << "error: " << db.lastError().text();
    }
    

The db.open() fails with the following message:

"[unixODBC][Driver Manager]Can't open lib 'SQL Native Client' : file not found QODBC3: Unable to connect"

My questions are:

I picked up the connection string from a forum post, I figured it was as good a place to start as any, but what exactly should be in there? Where does "SQL NAtive Client" come from? What do I need to do to setup my Qt / Linux box to be able to connect to a remote MS SQL Server?

like image 491
Michael Vincent Avatar asked Oct 14 '25 11:10

Michael Vincent


1 Answers

Sounds like you need to install the SQL Server ODBC Driver.

An explanation for how to do that is here:

  • https://technet.microsoft.com/en-us/library/hh568454(v=sql.110).aspx

In addition you need to refer to it by the correct name, which is "ODBC Driver 11 for SQL Server"

like image 83
Ben Avatar answered Oct 17 '25 00:10

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!