I just reinstalled QT because the debugger was simply not working to only find out that now the database connector is not working anymore. I did not change anything in the code regarding the connector and yet, it fails to connect me to the database.
This is the header:
<code>// databaseconnector.h
#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
class DatabaseConnector : public QObject
explicit DatabaseConnector(QObject *parent = nullptr);
bool connectToDatabase(const QString &serverName, const QString &dbName);
void disconnectFromDatabase();
QSqlDatabase getDatabase(){return m_database;};
#endif // DATABASECONNECTOR_H
<code>// databaseconnector.h
#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
class DatabaseConnector : public QObject
{
Q_OBJECT
public:
explicit DatabaseConnector(QObject *parent = nullptr);
~DatabaseConnector();
bool connectToDatabase(const QString &serverName, const QString &dbName);
void disconnectFromDatabase();
QSqlDatabase getDatabase(){return m_database;};
private:
QSqlDatabase m_database;
bool openConnection();
void closeConnection();
};
#endif // DATABASECONNECTOR_H
</code>
// databaseconnector.h
#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
class DatabaseConnector : public QObject
{
Q_OBJECT
public:
explicit DatabaseConnector(QObject *parent = nullptr);
~DatabaseConnector();
bool connectToDatabase(const QString &serverName, const QString &dbName);
void disconnectFromDatabase();
QSqlDatabase getDatabase(){return m_database;};
private:
QSqlDatabase m_database;
bool openConnection();
void closeConnection();
};
#endif // DATABASECONNECTOR_H
And this is the source file:
<code>// databaseconnector.cpp
#include "databaseconnector.h"
DatabaseConnector::DatabaseConnector(QObject *parent) : QObject(parent)
DatabaseConnector::~DatabaseConnector()
disconnectFromDatabase();
bool DatabaseConnector::connectToDatabase(const QString &serverName, const QString &dbName)
m_database = QSqlDatabase::addDatabase("QODBC");
QString dsn = QString("DRIVER={SQL Server};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(serverName).arg(dbName);
m_database.setDatabaseName(dsn);
void DatabaseConnector::disconnectFromDatabase()
bool DatabaseConnector::openConnection()
qDebug() << "Connected to database successfully!";
qDebug() << "Error connecting to database:" << m_database.lastError().text();
void DatabaseConnector::closeConnection()
if (m_database.isOpen()) {
qDebug() << "Disconnected from database";
<code>// databaseconnector.cpp
#include "databaseconnector.h"
#include <QDebug>
#include<QSqlError>
DatabaseConnector::DatabaseConnector(QObject *parent) : QObject(parent)
{
}
DatabaseConnector::~DatabaseConnector()
{
disconnectFromDatabase();
}
bool DatabaseConnector::connectToDatabase(const QString &serverName, const QString &dbName)
{
qDebug()<<serverName;
qDebug()<<dbName;
m_database = QSqlDatabase::addDatabase("QODBC");
QString dsn = QString("DRIVER={SQL Server};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(serverName).arg(dbName);
m_database.setDatabaseName(dsn);
return openConnection();
}
void DatabaseConnector::disconnectFromDatabase()
{
closeConnection();
}
bool DatabaseConnector::openConnection()
{
if (m_database.isOpen())
return true;
if (m_database.open()) {
qDebug() << "Connected to database successfully!";
return true;
} else {
qDebug() << "Error connecting to database:" << m_database.lastError().text();
return false;
}
}
void DatabaseConnector::closeConnection()
{
if (m_database.isOpen()) {
m_database.close();
qDebug() << "Disconnected from database";
}
}
</code>
// databaseconnector.cpp
#include "databaseconnector.h"
#include <QDebug>
#include<QSqlError>
DatabaseConnector::DatabaseConnector(QObject *parent) : QObject(parent)
{
}
DatabaseConnector::~DatabaseConnector()
{
disconnectFromDatabase();
}
bool DatabaseConnector::connectToDatabase(const QString &serverName, const QString &dbName)
{
qDebug()<<serverName;
qDebug()<<dbName;
m_database = QSqlDatabase::addDatabase("QODBC");
QString dsn = QString("DRIVER={SQL Server};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(serverName).arg(dbName);
m_database.setDatabaseName(dsn);
return openConnection();
}
void DatabaseConnector::disconnectFromDatabase()
{
closeConnection();
}
bool DatabaseConnector::openConnection()
{
if (m_database.isOpen())
return true;
if (m_database.open()) {
qDebug() << "Connected to database successfully!";
return true;
} else {
qDebug() << "Error connecting to database:" << m_database.lastError().text();
return false;
}
}
void DatabaseConnector::closeConnection()
{
if (m_database.isOpen()) {
m_database.close();
qDebug() << "Disconnected from database";
}
}
This is where I instantiate the connection with the database. Everything is the way I left it before reinstalling QT, I did not change a single word. The server name of the database is correct, the name of the database is correct.
<code>MainWindow::MainWindow(C_User* user, QWidget *parent)
// Încearcă să te conectezi la baza de date
if (m_dbConnector.connectToDatabase(R"(DESKTOP-RTLB2TISQLEXPRESS)", "Formula1nou")) {
qDebug() << "Connected to database successfully!";
qDebug() << "Failed to connect to database!";
// Inițializează interfața utilizatorului
// Configurări suplimentare
ui->toolBar->setMovable(false);
<code>MainWindow::MainWindow(C_User* user, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
// Încearcă să te conectezi la baza de date
if (m_dbConnector.connectToDatabase(R"(DESKTOP-RTLB2TISQLEXPRESS)", "Formula1nou")) {
qDebug() << "Connected to database successfully!";
} else {
qDebug() << "Failed to connect to database!";
}
// Inițializează interfața utilizatorului
ui->setupUi(this);
this->user = user;
// Configurări suplimentare
ui->toolBar->setMovable(false);
centerWindow(this);
}
</code>
MainWindow::MainWindow(C_User* user, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
// Încearcă să te conectezi la baza de date
if (m_dbConnector.connectToDatabase(R"(DESKTOP-RTLB2TISQLEXPRESS)", "Formula1nou")) {
qDebug() << "Connected to database successfully!";
} else {
qDebug() << "Failed to connect to database!";
}
// Inițializează interfața utilizatorului
ui->setupUi(this);
this->user = user;
// Configurări suplimentare
ui->toolBar->setMovable(false);
centerWindow(this);
}
The error I get:
Error connecting to database: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [Microsoft][ODBC Driver Manager] Invalid connection string attribute, IM002;01S00 QODBC: Unable to connect"
Thank you in advance for any help!