วันพฤหัสบดีที่ 23 มิถุนายน พ.ศ. 2559

Qt Database SQL Server


















========================================================================================

// .pro
QT         += core gui sql network


greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QtDB01

TEMPLATE = app

SOURCES += main.cpp\

        mainwindow.cpp


HEADERS  += mainwindow.h


FORMS    += mainwindow.ui

========================================================================================
// main.cpp  

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])


{

    QApplication a(argc, argv);

    MainWindow w;

    w.show();


    return a.exec();

}



// .h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H



#include <QMainWindow>

#include <QSqlQueryModel>

#include <QString>

#include <QtDebug>

#include <QtSql>

#include <QSqlQuery>



#include <QtGui>

#include <QtSql/QSqlQueryModel>

#include  <QtCore>



namespace Ui {

class MainWindow;

}


class MainWindow : public QMainWindow

{

    Q_OBJECT


public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();


private slots:

    void on_btnConnectDB_clicked();


    void on_btnQry_clicked();


private:

    Ui::MainWindow *ui;

    QSqlQueryModel *model;

    QSqlDatabase db;

};


#endif // MAINWINDOW_H


========================================================================================
// mainwindows.cpp


#include "mainwindow.h"

#include "ui_mainwindow.h"


QString servername = "BIG\\SQLEXPRESS";

QString dbname     = "test";

int cntData;


MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);



    db.setHostName(servername);

    db.setDatabaseName(dbname);


    QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";

    QString connectionString = connectionTemplate.arg(servername).arg(dbname);

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

    db.setDatabaseName(connectionString);


    db.open();


    if (db.open())

    {

        qDebug() << "Open";

        ui->TextEdit01->append("Open DB ");

    }


    else

    {

       qDebug() << "Error = " << db.lastError().text();

       ui->TextEdit01->append(db.lastError().text());

    }


     this->model = new QSqlQueryModel();


}


MainWindow::~MainWindow()

{

     db.close();

     qDebug() << "Closing . . . ";

     ui->TextEdit01->append("Closing...");

     delete ui;

}



void MainWindow::on_btnConnectDB_clicked()

{

        cntData++;

        QString cnt = QString::number(cntData);

        QString sqry = "INSERT INTO [dbo].[test_table]([firstname],[lastname]) VALUES (:firstname,:lastname)";

        QSqlQuery qry;

        qry.prepare(sqry);


         qry.bindValue(":firstname","BIG"+cnt);

         qry.bindValue(":lastname","BOM"+cnt);


        if(qry.exec())

        {

            ui->TextEdit01->append("Record ");

        }


}


void MainWindow::on_btnQry_clicked()

{


     model->setQuery("SELECT [firstname],[lastname] FROM [dbo].[test_table]");

     ui->tableView->setModel(model);

}





---------------------------------------------------------------------------------------


#Qry data from DB to tableView












https://doc.qt.io/qt-6/qsqlquery.html
https://www.youtube.com/watch?v=3XE2bKUAxfw&list=PL2D1942A4688E9D63&index=52
https://www.w3schools.com/sql/sql_where.asp

1 ความคิดเห็น:

เวลา 28 มกราคม 2565 เวลา 15:11 , Anonymous ไม่ระบุชื่อ กล่าวว่า...

Sands Casino | Las Vegas, NV | $10 Free + $2021 Casino
Las 바카라 사이트 Vegas Sands Casino offers everything you need 제왕 카지노 for an unforgettable Las Vegas stay. Enjoy daily 샌즈카지노 casino entertainment, dining, and more at our newly built

 

แสดงความคิดเห็น

สมัครสมาชิก ส่งความคิดเห็น [Atom]

<< หน้าแรก