vc.net中使用Qt连接SQLServer数据库
//1.添加SQL库:"Qt project setting"-->"Qt Modules",在SQL library复选框前打勾. //2.添加头文件 #include<QtSql> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlTableModel> #include<QtSql/QSqlError> //3.创建连接 qDebug()<<"available driver:"; QStringList drivers=QSqlDatabase::drivers(); foreach(QString driver,drivers) qDebug()<<"/t"<<driver; QSqlDatabase db=QSqlDatabase::addDatabase("QODBC3"); qDebug()<<"ODBC driver?"<<db.isValid(); QString dsn=QString::fromLocal8Bit("DRIVER={SQL SERVER};SERVER=192.168.0.123;DATABASE=test"); db.setHostName("192.168.0.123"); db.setDatabaseName(dsn); db.setUserName("sa"); db.setPassword("111111"); if(!db.open()) { qDebug()<<db.lastError().text(); QMessageBox::critical(0,QObject::tr("Database Error"),db.lastError().text()); return false; } //4.查询数据 QSqlQuery query; query.exec("select * from mytable"); while(query.next()) { ......... }