Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem: Connect with SQL to windows SQL server 2008 .mdf
QtWS25 Last Chance

Problem: Connect with SQL to windows SQL server 2008 .mdf

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 7.5k 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.
  • T Offline
    T Offline
    Tristan
    wrote on last edited by
    #1

    Good evening,

    I have some problems with connecting to an .mdf file with SQL in QT

    First i've made an window application in QT.
    then i've placed and .mdf database and a .ldf log file in my project directory.

    I wrote in the header file this code:

    @

    #include <QMainWindow>
    #include <QtSql>
    #include <QDir>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QSqlError>
    #include <QODBCDriver>

    private:

    Ui::MainWindow *ui;
    QSqlDatabase db;
    QSqlQuery query;@
    

    and in my cpp file i wrote the following code under a signalslot of a button:

    @void MainWindow::on_pushButton_clicked()
    {
    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("dbIT_Store.mdf");
    ui->lineEdit_2->setText(QString(query.exec("SELECT naam FROM tabel WHERE ID = '1'")));
    db.close();

    }@

    I just want to test, when i click on the button, if i can get some information out of my .mdf file and then print it in lineEdit2.
    I don't have any errors when i build my program and i have QOBDC installed for QT from my package manager.
    But when i push the button i got the following error:

    @QSqlQuery::exec: database not open@

    what am i doing wrong?

    thanks in advance!

    Greetings,
    Tristan

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      you should pass an ODBC connection string by setDatabaseName.
      take a look at "www.connectionstrings.com":http://www.connectionstrings.com and find a right ODBC connection string for your database.
      Probably you have to had SQL Server installed on your PC and mdf ldf should be attached.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsen
        wrote on last edited by
        #3

        i don't find db.open in your code! note that you can't access your database without opening a connection.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tristan
          wrote on last edited by
          #4

          Thanks for reply,

          I searched for my connection string en edited my code

          @void MainWindow::on_pushButton_clicked()
          {
          db = QSqlDatabase::addDatabase("QODBC");
          db.setDatabaseName("Driver={SQL Server Native Client 10.0};Server=.\SQLExpress;AttachDbFilename=/home/MlovesT/QT-programmas/SQL/dbIT_Store.mdf;Database=db; Trusted_Connection=Yes");
          db.open();
          ui->lineEdit_2->setText(QString(query.exec("SELECT Omschrijving FROM tblProducten WHERE ProductID = '1'")));
          db.close();

          }

          @

          But still got the same error.
          And the ldf and mdf file are 2 files from a friends computer (from windows SQL server 2008), i need these files for our project.
          He made these files on a windows PC, while im working on a Linux.

          I still don't really know what's wrong.

          Thanks in advance

          Greetings Tristan

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohsen
            wrote on last edited by
            #5

            are you using Linux OS? SQL Server only works on windows. then what's the AttachDBFilename on your connection string? just make your database attached on Enterprise Manager.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tristan
              wrote on last edited by
              #6

              Thanks for reply

              Why doesn't it work on an UNIX system? I have 3 drivers installed for ODBC on my Linux system: qt-sql-odbc, UNIXodbc and UNIXodbc-devel. So why can't i connect to an mdf database file with QT in Linux? I have no errors in QT that my drivers aren't installed.

              My attached file is: /home/MlovesT/QT-programmas/SQL/dbIT_Store

              But i want to connect to a local file, this has to be possible in some way? or am i wrong?

              Greetings,
              Tristan

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mohsen
                wrote on last edited by
                #7

                cuz SQL Server is only designed for Microsoft Windows. you should have SQLServer service running on your OS. Then you can access SQL Server engine by ODBC or directly using Jet OLEDB provider.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mohsen
                  wrote on last edited by
                  #8

                  why you don't use MySQL instead?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tristan
                    wrote on last edited by
                    #9

                    Okay, you helped very much because I now know I cant configure and OBCD server for a symbian device. I wanted to connect a symbian device to an MS server via SQL for our project, so i first tested OBCD on a desktop application. but now i guess that's impossible.

                    So we are solving the problem in another way... We have a visual basic program which can connect to an windows SQL server very well. So we are gonna run this visual basics program on the same server (same Ip) as the SQL server. Then the smartphones are going to connect to this Visual basic - server program trough TCP/IP, by sending an Query string. The visual basics server then sends a Query with the Query string from the smartphone, to the local MS SQL server etc etc....

                    So thanks for helping!

                    Greetings,
                    Tristan

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mohsen
                      wrote on last edited by
                      #10

                      It's ok. so you may use QTCPSocket for making your connection. But it gives a live connection. you may use SQLite if you wanted to make a cached database connection.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Tristan
                        wrote on last edited by
                        #11

                        Yes, we've tested QTCPSocket and its perfectly possible to send and receive data between our visual basic server. And what you're saying is true, so is will keep SQLite in mind and see what we can do to optimize our project.

                        Greetings,
                        Tristan

                        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