QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("mydb"); db.setUserName("uid"); db.setPassword("pwd"); bool connected = db.open(); if (!connected) { QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text()); return false; } QSqlQuery query; QString strQuery = "select * from mytable"; if(!query.exec(strQuery)) QMessageBox::critical(0, "Error", "Loading data failed", QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape, 0, 0); else { while(query.next()) { // код.... } }
C++ (Qt) bool connected = db.open(); if(!connected) { QMessageBox::Critical( 0, QObject::tr("Database Error"), db.lastError().text()); return false; }