#include <QCoreApplication> #include <QtSql> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString servername="THINKPAD-PC"; //LOCALHOST QString dbname="AdventureWorks2008R2"; QSqlDatabase db=QSqlDatabase::addDatabase("QODBC"); db.setConnectOptions(); QString dsn=QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname); db.setDatabaseName(dsn); if(db.open()) { qDebug()<<"opened"; db.close(); } else { qDebug()<<"ERROR="<<db.lastError().text(); } return a.exec(); }
时间: 2024-10-20 08:29:02