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

 

 

 

 

特别注意 sql2005         << ).arg("20100720-1406\\SQLEXPRESS")>>

posted @ 2010-07-20 22:37  MokLiu  阅读(1754)  评论(0编辑  收藏  举报