Qt中通过ODBC连接MSSQL数据库

/************************************************************/

#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>


static bool connection()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName(QString("DRIVER={SQL SERVER};"
                               "SERVER=%1;" //服务器名称
                               "DATABASE=%2;"//数据库名
                               "UID=%3;"           //登录名
                               "PWD=%4;"        //密码
                               ).arg("TANY_PC")
                                .arg("MyData")
                                .arg("sa")
                                .arg("")
                       );
    if (!db.open())
    {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            db.lastError().databaseText(), QMessageBox::Cancel);
        return false;
    }
    else
    {
        QMessageBox::information(0,"Connect Database","connect success!",QMessageBox::Ok);
        return true;
    }
    return true;
}
#endif // CONNECTION_H

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxssj.html