Hi All,
I was using QODBC and set up a DSN to access a Microsoft Access (.mdb) database without any problem. My Qt version is 4.6.3. Now I would like to deploy the app. and avoid requiring users to set up a DSN. I found out that you can use a connection string instead. Please search for "connection string" in below page.
http://doc.qt.nokia.com/4.6/qsqldata...l#QSqlDatabase
Here is the DSN based code.
//----Initialize Database----
_dbDriver = "QODBC";
_dbDSN = "DecisionMakingStudy";
_dbHost = "localhost";
_dbUser = "";
_dbPasswd = "";
_db = QSqlDatabase::addDatabase(_dbDriver);
_db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_O V_ODBC3");//force ODBC3.x behavior
_db.setHostName(_dbHost);
_db.setUserName(_dbUser);
_db.setPassword(_dbPasswd);
_db.setDatabaseName(_dbDSN);
if(!_db.open()){
_db.setConnectOptions();// clear options
qFatal(_db.lastError().text());
}
...
Here is the connection string based code which is *failed* in _db.open() with error
"[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"
_db = QSqlDatabase::addDatabase(_dbDriver);
_db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_O V_ODBC3");//force ODBC3.x behavior
QString databaseName = "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};UID=;PWD=;DBQ=test.mdb";
_db.setDatabaseName(databaseName);
if(!_db.open()){
_db.setConnectOptions();// clear options
qFatal(_db.lastError().text());
}
...
The only difference between the two methods is highlighted in red. The .mdb file located at the project root directory. Using the absolute file path in connection string method did not help. Also connecting to a copy of the DSN linked .mdb (rather than the linked .mdb itself worrying DSN setup might prevent external access) failed as well.
Would you please help? Thanks so much.
Best,
dm
Bookmarks