Qt连接Access数据库,出现错误“QSqlQuery::exec: database not open”
Qt连接Access数据库,出现错误“QSqlQuery::exec: database not open”,不知道哪里出错了,为什么不能打开数据库?代码如下:
#include "dbconnection.h"
dbconnection::dbconnection()
{
}
QSqlDatabase dbconnection::createConnection()
{
static int cCount = 0;
QSqlError err;
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", QString("conn%1").arg(++cCount));
//strDataBaseName = QString("conn%1").arg(++cCount);
db.setDatabaseName("llj");
db.setHostName("127.0.0.1");
db.setPort(1000);
if (false == db.open("sa", "sa"))
{
err = db.lastError();
db = QSqlDatabase();
//ShowCriticalMessageBox();
QSqlDatabase::removeDatabase(QString("conn%1").arg(cCount));
}
if (err.type() != QSqlError::NoError)
{
//ShowCriticalMessageBox();
}
return db;
}
void dbconnection::ShowCriticalMessageBox()
{
QMessageBox::critical( 0
, qApp->tr("Cannot open database")
, qApp->tr("Click Cancel to exit.")
, QMessageBox::Cancel );
}