• 4722阅读
  • 2回复

QT 4.4.3 connects SQL server 2005 error [复制链接]

上一主题 下一主题
离线ningfeng8899
 

只看楼主 倒序阅读 楼主  发表于: 2011-12-09
It always popup a the dialog "Driver not  loaded Driver not loaded" I had copy all dirvers (QTDIR / plugins/ sqldrivers)  4 dll(qsqlite4.dll qsqlited4.dll,
qsqlodbc4.dll,qsqlodbc4d.dll) to my local debug directory (debug/sqldirvers/).


At first, In .Pro file I add the one line "LIBS += QtSql.lib"


bool createConnection()
{
  QSqlQuery query;
bool bResult = false;
QSqlDatabase db = QSqlDatabase::addDatabase("ODBC","staffManager") // staffManager is my database name.
db.setHostName("sever"); // sever is my server name;
db.setDatabaseName("Pet");
if(!db.open()) // it can open database but can not query table it says "Driver not  loaded Driver not loaded"
{
  QMessage::about (0,Object::tr("DataBase error"),db.lastError().text());
  return false;
}
else
{
  bResult = query.exec("select * from department");// department is table of database staffManager ; // bResult is false
  if(!query.isActive())
  {
   QMessage::warning(0,Object::tr("DataBase error"),db.lastError().text()); // here it popup the error info."Driver not  loaded Driver not loaded"
  }


}
}


I hope experts can give me some good solution.
Thank you.
离线wxj120bw

只看该作者 1楼 发表于: 2011-12-09
回 楼主(ningfeng8899) 的帖子
you call the function QSqlDatabase::drivers () to get the available drivers in qt Currently. if the sql server driver is not in the result, you can compile the sql server driver.
离线ningfeng8899

只看该作者 2楼 发表于: 2011-12-09
Thanks. The actually reason is that I must invoked functions with following method.

// at first relates your database name;
QSqlDatabase db = QSqlDatabase ::database(databasename);
// databasename is your database name.
QSqlQuery _query = QSqlQuery("select * from tablename",db);
// db is your specified database.
快速回复
限100 字节
 
上一个 下一个