QT 5 通过ODBC连接SQL SERVER

2020-11-03 10:16:58  阅读 2651 次 评论 0 条

学习笔记:

1:找个SQL server 数据库  我就拿我的财务辅助系统开刀了

2:  windows上建立ODBC了 这个就不多写了

3:qt新建个工程 再PRO文件里加上

QT       += sql

下面是一部分代码

#include "widget.h"
#include "ui_widget.h"
#include <QApplication>
#include <QtSql/QSql>
#include <QDialog>
#include <QDebug>
#include <QMessageBox>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>

bool OpenDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");   //数据库驱动类型为SQL Server
    QString dsn = QString::fromLocal8Bit("dfcwfz");      //数据源名称
    db.setHostName("192.168.210.3");                        //选择SQL SERVER服务器
    db.setDatabaseName(dsn);                            //设置数据源名称
    db.setUserName("sa");                               //登录用户
    db.setPassword("*******");                              //密码
    if(!db.open())                                      //打开数据库
    {
        qDebug()<<db.lastError().text();
        QMessageBox::critical(0, QObject::tr("数据库打开失败!"), db.lastError().text());
        return false;                                   //打开失败
    }
    else
    {
        qDebug()<<"数据库打开成功!";
        QSqlQuery query(db); //查询  系统参数  表并输出,测试能否正常操作数据库
        query.exec("SELECT * FROM  系统参数");
        while(query.next())
        {
            qDebug()<<query.value(0).toInt() <<query.value(1).toString() <<query.value(2).toInt();
        }

    }return true;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    OpenDatabase();
}

Widget::~Widget()
{
    delete ui;
}

一下为运行结果

image.png


进阶版:

使用全局变量:

头文件里

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtSql/QSql>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlQueryModel>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    static QSqlDatabase db ;  //定义全局变量

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H




这里是使用了<QTableWidget> 来显示表格里的数据 
全局变量作为数据库链接,调用方便一点

#include "widget.h"
#include "ui_widget.h"
#include <QApplication>
#include <QTableWidget>
#include <QDialog>
#include <QDebug>
#include <QMessageBox>

QSqlDatabase Widget::db = QSqlDatabase::addDatabase("QODBC");
bool OpenDatabase()
{
     //数据库驱动类型为SQL Server
    qDebug()<<"ODBC driver?"<<Widget::db.isValid();
    QString dsn = QString::fromLocal8Bit("dfcwfz");      //数据源名称
    Widget::db.setHostName("192.168.210.3");                        //选择数据库
    Widget::db.setDatabaseName(dsn);                            //设置数据源名称
    Widget::db.setUserName("sa");                               //登录用户
    Widget::db.setPassword("*********");                              //密码
    if(!Widget::db.open())                                      //打开数据库
    {
        qDebug()<<Widget::db.lastError().text();
        QMessageBox::critical(0, QObject::tr("打开数据库失败!!"), Widget::db.lastError().text());
        return false;                                   //打开失败
    }
    else
    {
        qDebug()<<"打开数据库成功!";

    }return true;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //显示QtableWidget
    //加水平表头

     //打开数据库
    if(OpenDatabase()){
        //成功执行查询语句
        QSqlQuery query(Widget::db); //查询 表并输出,测试能否正常操作数据库
        query.exec("SELECT * FROM  系统参数");
        QStringList list1;
        list1.clear();
        list1<<"类型"<<"名称"<<"说明"<<"值";
        ui->tableWidget->setColumnCount(list1.size());
        ui->tableWidget->setHorizontalHeaderLabels(list1);

        int row=0;
        ui->tableWidget->setRowCount(20); //暂时设置成20行
        while(query.next())
        {
            ui->tableWidget->setItem(row,0,new QTableWidgetItem(query.value(1).toString()));
            ui->tableWidget->setItem(row,1,new QTableWidgetItem(query.value(2).toString() ));
            ui->tableWidget->setItem(row,2,new QTableWidgetItem(query.value("说明").toString()));
            ui->tableWidget->setItem(row,3,new QTableWidgetItem(query.value("值").toString() ));
            row++;
        }
    }


}

Widget::~Widget()
{
    delete ui;
}

运行结果如图:

image.png

本文地址:http://blog.jinesc.net/?id=212
版权声明:本文为原创文章,版权归 jinesc 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?