4

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?

1
  • compile qodbc from source directory of Qt installation and see this page for connection strings: wiki.qt.io/ODBC Commented Sep 24, 2015 at 11:22

1 Answer 1

4

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

An explanation for how to do that is here:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @Ben. Is there a way of knowing if the ODBC driver has already been installed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.