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>();
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++11greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = shop
TEMPLATE = appSOURCES += main.cpp
mainwindow.cpp
cborderedwidget.cpp
cinfowidget.cpp
dbconnector.cppHEADERS += mainwindow.h
cborderedwidget.h
cinfowidget.h
dbconnector.hLIBS += 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 and welcome to devnet,
Do you have any error text to show ?
-
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
-
Is this a PlugIns issue ? You need to put the required plugin dlls into the right folder :-
...<folder with your exe>/sqldrivers/qsqlodbc.dllThen 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- qmake
- jom
TEMPLATE = app
TARGET = shopCONFIG += console c++11 import_plugins release
QT += gui sql widgetsQTPLUGIN += 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 -
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
-
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 connectionafter 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 :-) -
Sorry, I meant QODBC
[Solved]