Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MSSQL Driver not loaded

MSSQL Driver not loaded

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nonameletters
    wrote on last edited by
    #1

    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>();
    m_db->addDatabase("QODBC");

        QStringList drivers = QSqlDatabase::drivers();
    
        cout << "Drivers count: " << drivers.size() << endl;
        for (int i = 0; i < drivers.size(); i++)
        {
            cout << "Driver: " << drivers.at(i).toStdString() << endl;
        }
    
        // this are variants that i used
        // m_db->setHostName("192.168.11.109\\Legos");
        // m_db->setDatabaseName("Driver={SQL Server Native Client 10.0};Server=192.168.11.109\\Legos;Database=FLEX_DB;");
        // m_db->setDatabaseName("Driver={SQL Server Native Client 10.0};Server=Legos;Database=FLEX_DB;");
        // m_db->setDatabaseName("FLEX_DB");
    
        m_db->setDatabaseName(tr("Legos"));
        m_db->setUserName(tr("sa"));
        m_db->setPassword(tr("hizhechu"));
    
        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
    mainwindow.cpp
    cborderedwidget.cpp
    cinfowidget.cpp
    dbconnector.cpp

    HEADERS += mainwindow.h
    cborderedwidget.h
    cinfowidget.h
    dbconnector.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]

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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=192.168.11.109;Database=FLEX_DB”);
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Do you have any error text to show ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nonameletters
          wrote on last edited by
          #4

          There were coments lines.

          This is working code.
          m_db->setDatabaseName(tr("Legos"));
          m_db->setUserName(tr("sa"));
          m_db->setPassword(tr("hizhechu"));

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

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vaquita Tim
            wrote on last edited by
            #5

            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: " << ...@

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nonameletters
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nonameletters
                wrote on last edited by
                #7

                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

                INCLUDEPATH += .
                "c:\soft\Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtWidgets\"
                "c:\soft\Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtSql\"

                Input

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

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  Vaquita Tim
                  wrote on last edited by
                  #8

                  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");
                  //db.setHostName("DESK\SQLEXPRESS");
                  m_Database.setDatabaseName("GAIThouseODBC"); // ODBC name - all other details are set up already in the ODBC config
                  //db.setUserName("Tim");
                  //db.setPassword("Password");
                  m_Database.open();

                  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

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nonameletters
                    wrote on last edited by
                    #9

                    I found the in what I was mistaken.
                    The code was:
                    m_db = std::make_shared<QSqlDatabase>();
                    m_db->addDatabase("QODBC");
                    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.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Vaquita Tim
                      wrote on last edited by
                      #10

                      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 :-)

                      1 Reply Last reply
                      0
                      • hskoglundH Offline
                        hskoglundH Offline
                        hskoglund
                        wrote on last edited by
                        #11

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

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

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nonameletters
                          wrote on last edited by
                          #12

                          Sorry, I meant QODBC
                          [Solved]

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved