Important: Please read the Qt Code of Conduct -

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

  • 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>


    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");
    ui->lineEdit_2->setText(QString(query.exec("SELECT naam FROM tabel WHERE ID = '1'")));


    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!


  • you should pass an ODBC connection string by setDatabaseName.
    take a look at "": 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.

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

  • 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");;
    ui->lineEdit_2->setText(QString(query.exec("SELECT Omschrijving FROM tblProducten WHERE ProductID = '1'")));



    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

  • 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.

  • 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?


  • 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.

  • why you don't use MySQL instead?

  • 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!


  • 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.

  • 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.


Log in to reply