PDA

View Full Version : QSqlDatabase Connection Close on Destruction



Sanuden
22nd July 2010, 21:11
I'm wondering if anyone has else has seen this issue when using Qt to connect to a MS SQL database. The connection is opened fine and I am able to access data without issues, however, when the application terminates (and thus, the QSqlDatabases that were constructed through addDatabase are destroyed) I get this message:


"QODBCDriver::disconnect: Unable to disconnect datasource" Error: " [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionClose (closesocket()). [Microsoft][ODBC SQL Server Driver][DBNETLIB]" "QODBCDriver::cleanup: Unable to free connection handle" Error: " [Microsoft][ODBC Driver Manager] Function sequence error" "QODBCDriver::cleanup: Unable to free environment handle" Error: "[Microsoft][ODBC Driver Manager] Function sequence error "

However, if I call QSqlDatabase::remove() on each database name that I had created throughout the lifetime of the application, the program terminates fine and I don't see the message. Any ideas as to why I need to close each connection manually to prevent this error?

m0n0g0n
1st September 2011, 16:32
Same problem. can anyone help?

Same problem. can anyone help?

Added after 5 minutes:

This is my code:



int main(int argc, char *argv[]) {
QSettings* pSettings = new QSettings(QDir::currentPath() + "/" + CONFIG_FILE, QSettings::IniFormat);
QSettings& settings = *pSettings;

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString connectionTemplate = "Driver={SQL Server};Server=%1;Database=%2;Uid=%3;Pwd=%4;";

QString server = settings.value("DBProductsServer").toString();
QString database = settings.value("DBProductsDatabase").toString();
QString user = settings.value("DBProductsUser").toString();
QString pass = settings.value("DBProductsPass").toString();

QString connectionString = connectionTemplate
.arg(server)
.arg(database)
.arg(user)
.arg(pass);

db.setDatabaseName(connectionString);

if (db.open() ) {
std::cout << "DB Verbindung vorhanden" << std::endl;
QSqlQuery query("SELECT * FROM dbo.BOM");

while (query.next()) {
std::cout << query.value(0).toString().toStdString() << std::endl;
}
} else {
QSqlError error = db.lastError();
std::cout << error.databaseText().toStdString() << std::endl; ;
std::cout << error.driverText().toStdString() << std::endl; ;

std::cout << "Konnte keine DB Verbindung aufbauen" << std::endl;
}

return 0;
}



After execution I see the following error messages:


"QODBCDriver::disconnect: Unable to disconnect datasource" Error: " [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionClose (closesocket()). [Microsoft][ODBC SQL Server Driver][DBNETLIB]"
"QODBCDriver::cleanup: Unable to free connection handle" Error: " [Microsoft][ODBC Driver Manager] Fehler in der Funktionsreihenfolge"
"QODBCDriver::cleanup: Unable to free environment handle" Error: "[Microsoft][ODBC Driver Manager] Fehler in der Funktionsreihenfolge "