I'm trying to connect with a database created with Microsoft Sql Express 2012. I'm using qt 5.2.1, and at the end of the connection implementetion I write some code to have a validation. But an error accure, and I don't know why. The program is structured in this way:
-in the MainWindow of qt GUI application there is a Button, if you click on this Button you open a Dialog Application. -in the Dialog Application is devolped the connection.
here the .h code of the Dialog Application:
#ifndef DATABASE_H
#define DATABASE_H
#include <QDialog>
namespace Ui {
class DataBase;
}
{
Q_OBJECT
public:
explicit DataBase
(QWidget *parent
= 0);
~DataBase();
private:
Ui::DataBase *ui;
};
#endif // DATABASE_H
#ifndef DATABASE_H
#define DATABASE_H
#include <QDialog>
namespace Ui {
class DataBase;
}
class DataBase : public QDialog
{
Q_OBJECT
public:
explicit DataBase(QWidget *parent = 0);
~DataBase();
private:
Ui::DataBase *ui;
};
#endif // DATABASE_H
To copy to clipboard, switch view to plain text mode
here the relative .cpp code of Dialog Application:
#include <database.h>
#include <ui_database.h>
#include <QtSql>
#include <QtDebug>
#include <iostream>
DataBase
::DataBase(QWidget *parent
) : ui(new Ui::DataBase)
{
db.setConnectOptions();
QString dsn
= QString("DRIVER={SQL Native Client}; SERVER=%1; DATABASE=%2;TRUSTED_CONNECTION=Yes;").
arg(servername
).
arg(dbname
);
db.setDatabaseName(dsn);
if(db.open()){
ui->label->setText("Connected....");
}
}
DataBase::~DataBase()
{
delete ui;
}
#include <database.h>
#include <ui_database.h>
#include <QtSql>
#include <QtDebug>
#include <iostream>
DataBase::DataBase(QWidget *parent) :
QDialog(parent),
ui(new Ui::DataBase)
{
QString servername="MARCO-VAIO";
QString dbname="IfaCOM";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Native Client}; SERVER=%1; DATABASE=%2;TRUSTED_CONNECTION=Yes;").arg(servername).arg(dbname);
db.setDatabaseName(dsn);
if(db.open()){
ui->label->setText("Connected....");
}
}
DataBase::~DataBase()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
and here the error that accured:
Cattura.JPG
Please check this issue...thanks in advance.
Bookmarks