Qt5.6连接SQL Server2012遇ODBC驱动未加载问题求助
Hey there, let's work through this ODBC driver issue together—this is a common gotcha with Qt and SQL Server, especially on 32-bit systems. Here are the most likely fixes based on your setup:
1. Make sure Qt's 32-bit ODBC driver files exist
Qt 5.6 needs specific 32-bit driver files to load QODBC. Check your Qt installation directory for these two files:
qsqlodbc.dll(release version)qsqlodbcd.dll(debug version)
They should be located in:
- For MinGW:
Qt安装目录\5.6\mingw492_32\plugins\sqldrivers - For MSVC:
Qt安装目录\5.6\msvc2013\plugins\sqldrivers
If they're missing, either:
- Re-run the Qt installer and make sure you check the ODBC driver component under the 32-bit Qt version, or
- Copy the files from a working 32-bit Qt 5.6 installation (match the compiler type—MinGW vs MSVC!).
2. Verify you have the 32-bit SQL Server ODBC driver installed
Even if you installed the SQL Server ODBC driver, double-check it's the 32-bit version (critical for your 32-bit OS). To confirm:
- Open the 32-bit ODBC Data Source Manager by running
C:\Windows\SysWOW64\odbcad32.exe(on 32-bit Windows, this is the defaultodbcad32.exe, but it never hurts to use the full path) - Go to the Drivers tab and look for entries like
SQL ServerorSQL Native Client 11.0(the latter is the official driver for SQL Server 2012). If it's missing, download and install the 32-bit SQL Server 2012 ODBC driver.
3. Ensure you're using a 32-bit Qt Kit in Qt Creator
Qt Creator might accidentally pick a 64-bit kit even on a 32-bit system. To check:
- Go to
Tools > Options > Build & Run > Kits - Select the kit you're using for your project
- Confirm the Qt version is 32-bit (e.g.,
Qt 5.6.1 MinGW 32bit) and the Compiler is a 32-bit toolchain (e.g.,MinGW 4.9.2 32bit)
4. Help Qt find the ODBC driver plugins
Sometimes Qt can't locate the sqldrivers folder automatically. Fix this by:
- Adding the plugin path to your code at startup:
QCoreApplication::addLibraryPath(QApplication::applicationDirPath() + "/sqldrivers"); - Or, copying the entire
sqldriversfolder from your Qt installation into your project's debug/release output directory (where your.exefile lives).
5. Test driver loading with a debug check
Add this line before opening the database to see which drivers Qt can actually load:
qDebug() << "Available drivers:" << QSqlDatabase::drivers();
If QODBC still doesn't show up, go back to step 1—your Qt driver files are either missing or in the wrong place.
Example Fixed Code
Here's your code with the plugin path check added:
#include <QCoreApplication> #include <QSqlDatabase> #include <QSqlError> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Add plugin path if needed QCoreApplication::addLibraryPath(QApplication::applicationDirPath() + "/sqldrivers"); QString servername = "AFGHAN-PC"; QString dbname = "try"; QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setConnectOptions(); QString dsn = QString("DRIVER={SQL Server};SERVER=%1;Database=%2;Trusted_Connections=Yes;").arg(servername).arg(dbname); db.setDatabaseName(dsn); // Debug: list available drivers qDebug() << "Available drivers:" << QSqlDatabase::drivers(); if(db.open()) { qDebug() << "Openned!"; db.close(); } else { qDebug() << "Error = " << db.lastError().text(); } return a.exec(); }
内容的提问来源于stack exchange,提问作者Ahmad Afghan

