bool MainWindow::openConnection() ... { QString accessFile("../guestdb.mdb"); QString dsn = QString( "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=%1").arg(accessFile); const QString PREFIX("puremilk.access.connection"); QString connID = PREFIX + "-" + accessFile; db = QSqlDatabase::database(connID, false); if( db.isValid() )...{ if (db.isOpen())...{ ui.edOdbcText->setText("Already opened."); return true; } } db = QSqlDatabase::addDatabase("QODBC", connID); db.setDatabaseName(dsn); return db.open();} void MainWindow::on_btnOdbcDemo_clicked() ... { if(!openConnection())...{ ui.edOdbcText->setText("openConnection() return false."); } QSqlQuery query(db); if ( ! query.exec( "SELECT * FROM guestbook " ) )...{ ui.edOdbcText->setText("query() failed."); return ; } dbModel = new QSqlQueryModel(this); dbModel->setQuery( query ); ui.tableView1->setModel( dbModel );} 同样的,连接 SQL SERVER 或者 其它的数据库也是相似的。 只是驱动不相同,修改连接字符串就好了。