Important: Please read the Qt Code of Conduct -

MSSQL Driver not loaded

  • Hello, I can't connect to Database (MS SQL 2008 R2). Here is my code. As a result I have Driver not loaded. I've red some topics, but no results.
    First, I tried to configure DNS, using windows GUI I connect to DB. I've checked connection by using osql it is OK. But running my program with this parametrs i can't get connection.

    m_db = std::make_shared<QSqlDatabase>();

        QStringList drivers = QSqlDatabase::drivers();
        cout << "Drivers count: " << drivers.size() << endl;
        for (int i = 0; i < drivers.size(); i++)
            cout << "Driver: " << << endl;
        // this are variants that i used
        // m_db->setHostName("\\Legos");
        // m_db->setDatabaseName("Driver={SQL Server Native Client 10.0};Server=\\Legos;Database=FLEX_DB;");
        // m_db->setDatabaseName("Driver={SQL Server Native Client 10.0};Server=Legos;Database=FLEX_DB;");
        // m_db->setDatabaseName("FLEX_DB");
        bool res = m_db->open(tr("sa"),tr("hizhechu"));
        QSqlError l_error = m_db->lastError();
        cout << "The result is1: " << res << " error: " << l_error.databaseText().toStdString() << std::endl;

    My pro file

    Project created by QtCreator 2014-01-30T17:08:08


    QT += core gui sql widgets
    CONFIG += console c++11

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = shop
    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS += mainwindow.h

    LIBS += c:\soft\Qt\Qt5.2.0\5.2.0\mingw48_32\plugins\sqldrivers\qsqlodbc.dll

    FORMS += mainwindow.ui

    wince*: {
    DEPLOYMENT_PLUGIN += qsqlite

    [edit: Added missing coding tags @ SGaist]

  • Hi, I think calling both setHostName() and setDatabaseName isn't good, also why 3 calls to setDatabaseName(), you only need one, try something like this:
    m_db->setDatabaseName(“Driver=SQL Server; Server=;Database=FLEX_DB”);

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you have any error text to show ?

  • There were coments lines.

    This is working code.

    I used SqlBrowser application from examples. It connects to db! But mine no. I can't undersatd what i need

  • Is this a PlugIns issue ? You need to put the required plugin dlls into the right folder :-
    ...<folder with your exe>/sqldrivers/qsqlodbc.dll

    Then you ought to see QODBC in the output from
    @cout << "Driver: " << ...@

  • I see avalible dirvers! But the same! How i can get more information about falt?

  • I have changed pro file and add static plugin. But the executable file size staied the same. What am I doing wrong?
    in the project dir

    1. qmake
    2. jom

    TEMPLATE = app
    TARGET = shop

    CONFIG += console c++11 import_plugins release
    QT += gui sql widgets

    QTPLUGIN += qsqlodbc

    DEPLOYMENT_PLUGIN += qsqlodbc

    DESTDIR += exec



    HEADERS += cborderedwidget.h cinfowidget.h dbconnector.h mainwindow.h
    FORMS += mainwindow.ui
    SOURCES += cborderedwidget.cpp

  • Ok. Here's my code which works with MSSQLExpress2012 and 2008 (Qt 5.3 & vs2013).

    @ // Sets up the default database for the SQL connection object
    m_Database = QSqlDatabase::addDatabase("QODBC");
    m_Database.setDatabaseName("GAIThouseODBC"); // ODBC name - all other details are set up already in the ODBC config

    QSqlQuery Query;
    Query.exec("USE GAIThouse" );

    Notice the "DatabaseName" is the name you give to the configuration of the DSN you set up in the Data Source Admin tool on your local machine, and not the name of the database in the server.

    Also, I issue an SQL "USE" command but I think this is redundant (I think it was required once in a previous project, and it lingers, without seeming to do any harm).

    I suggest putting your lastError() output after every step, which should help pinpoint the problem.

    As a last resort you might try switching on the logging in the ODBC DSN drivers (though if this is not loaded, this won't help !).

    Usually you can get these to work, so keep at it. Still, I have seen problems getting data from a database via ODBC using Qt where it all worked without problem in MS Word. I never figured it out but something going on under the hood there if you ask me

  • I found the in what I was mistaken.
    The code was:
    m_db = std::make_shared<QSqlDatabase>();
    addDatabase is static method it creates and returns connection

    after I changed to the next code id starts to work

    m_db = QSqlDatabase::addDatabase("ODBC");

    After that m_db is assosiated with driver and we can use it.

  • Aha ! Well done ! ( I didn't understand what the std::make_shared was supposed to be doing...).
    I have to say I find the "hidden" database connection paradigm a bit weird - in the end you just have to follow the examples carefully, rather than trying to be intelligent about it - but never mind.
    Don't forget to add [Solved] to the title :-)

  • Just to be sure, did this really work for you:
    m_db = QSqlDatabase::addDatabase(“ODBC”);

    m_db = QSqlDatabase::addDatabase(“QODBC”); ?

  • Sorry, I meant QODBC

Log in to reply