Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Collegamento SQL server 2008 MS

Collegamento SQL server 2008 MS

Scheduled Pinned Locked Moved Solved Italian
7 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Polly
    wrote on 6 Nov 2018, 09:25 last edited by
    #1

    Ho seguito un tutorial on line per la creazione di un programma che si occupa di interrogare un database SQL SERVER 2008
    e quando lo compilo mi scrive questo

    QSqlDatabase: QOBDC3 driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
    

    uso questo costruttore

    DatabaseConnection::DatabaseConnection(const  QString &server,
                                           const QString &driver,
                                           const QString &user,
                                           const QString &password,
                                           const QString &databaseName,
                                           bool trustedConnection)
    {
       mDatabase = QSqlDatabase::addDatabase("QOBDC3") ;
       mDatabaseName = databaseName;
       mServer = server;
       mDriver = driver;
       mUser = user;
       mPassword=password;
       mTrustedConnection = trustedConnection;
    }
    
    

    a cui passo i seguenti parametri

     mDbConnection("Nome Server",
                      "SQL Server",
                     "sa",
                      "password",
                      "nome database",
                      true)
    
    

    Utilizzo per questo windows 10
    e vorrei anche precisare che il nome server è
    PC\PIPPO, quindi vorrei chiedere come inserire il \ nello schema

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 6 Nov 2018, 11:33 last edited by
      #2

      Hi installato un driver ODBC sulla tua macchina?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Polly
        wrote on 6 Nov 2018, 13:14 last edited by Polly 11 Jun 2018, 14:12
        #3

        Si sono installati sia a 32 che 64 bit
        e ho trovato questi due tipi:
        SQL Server
        SQL Server Native Client 10.0

        con i relativi driver
        DRIVER={SQL Server}
        DRIVER={SQL Server Native Client 10.0}

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 7 Nov 2018, 10:34 last edited by
          #4

          Puoi, invece di puntare al tuo database, provare ad aprire un file excel con odbc (il come e' descritto qui: https://wiki.qt.io/Handling_Microsoft_Excel_file_format)

          Voglio capire se il problema e' nel setup o nell'individuare il DB

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Polly
            wrote on 9 Nov 2018, 08:44 last edited by
            #5

            Ho modificato il programma come mi hai consigliato, però mi dà lo stesso errore. Ti metto tutto il codice completo

            databaseconnectio.h

            #ifndef DATABASECONNECTION_H
            #define DATABASECONNECTION_H
            
            #include <QSqlDatabase>
            
            
            class DatabaseConnection
            {
            public:
                DatabaseConnection(
                                   const QString &driver,
                                   const QString &dbq);
                bool openDatabase(QString *error = nullptr);
            private:
                QSqlDatabase mDatabase;
                QString mDbq;
                QString mDriver;
            };
            
            #endif // DATABASECONNECTION_H
            

            databaseconnection.cpp

            #include "databaseconnection.h"
            #include <QSqlQuery>
            #include <QSqlError>
            
            DatabaseConnection::DatabaseConnection(
                                                   const QString &driver,
                                                   const QString &dbq)
            {
               mDatabase = QSqlDatabase::addDatabase("QOBDC","xslx_connection") ;
               mDbq = dbq;
               mDriver = driver;
            }
            
            bool DatabaseConnection::openDatabase(QString *error)
            {
                mDatabase.setDatabaseName(QString("DRIVER={%1};DBQ=%2")
                                          .arg(mDriver)
                                         .arg(mDbq));
                if (!mDatabase.open()){
                    if (error != nullptr){
                        *error = mDatabase.lastError().text();
                    };
                return false;
                };
                    return true;
            }
            
            

            mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include "databaseconnection.h"
            
            namespace Ui {
            class MainWindow;
            }
            
            class QSqlQueryModel;
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
            private slots:
                void on_actionUscire_dal_programma_triggered();
            
                void on_actionRiguardo_a_Qt_triggered();
            
                void on_actionAccesso_a_Microsoft_SQL_Server_triggered();
            
            private:
                Ui::MainWindow *ui;
                DatabaseConnection mDbConnection;
                QSqlQueryModel * mModel;
            };
            
            #endif // MAINWINDOW_H
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QMessageBox>
            #include <QSqlQueryModel>
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow),
                mDbConnection("Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)",
                              "C:\\percorso\\file\\XLS\\file.xlsx")
            {
                ui->setupUi(this);
                mModel = nullptr;
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_actionUscire_dal_programma_triggered()
            {
               close();
            }
            
            void MainWindow::on_actionRiguardo_a_Qt_triggered()
            {
                QMessageBox::aboutQt(this,"Qt");
            }
            
            void MainWindow::on_actionAccesso_a_Microsoft_SQL_Server_triggered()
            {
                QString error;
                if (!mDbConnection.openDatabase(&error)){
                    QMessageBox::critical(this,"Error",error);
                    return;
                };
                if (mModel == nullptr){
                    mModel = new QSqlQueryModel(this);
                    mModel->setQuery ("select * from [" + QString("Sheet1") + "$A4:B5]");
                    ui->tableView->setModel(mModel);
                }
                else
                {
                    mModel->setQuery ("select * from [" + QString("Sheet1") + "$A4:B5]");
                }
            }
            
            

            main.cpp

            #include "mainwindow.h"
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                a.setStyle("fusion");
                MainWindow w;
                w.show();
            
                return a.exec();
            }
            
            
            1 Reply Last reply
            0
            • P Offline
              P Offline
              Polly
              wrote on 28 Nov 2018, 10:40 last edited by
              #6

              Ma stavo pensando non è che devo magari aggiungere un file .dll da aggiungere nella cartella della compilazione?
              Anche perchè sto provando appunto anche ad agire su un file excel e credo che mi dia lo stesso problema.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Polly
                wrote on 28 Nov 2018, 16:00 last edited by
                #7

                Risolta la frittata. Negli strumenti di amministrazione ho letto per esteso il nome dei driver e una volta corretto si è messo ad andare.

                Nel mio caso era

                SQL Server Native Client 10.0
                

                Grazie ancora per l'attenzione.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved