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