QtSQL Connect to DataBase

92 views Asked by At

I have this project in Visual Studio, it's written in C++, the database it's provided by SQL Server and I'm using Qt VS Tools. The problem is that I want to connect to a database with QtSql function, but it's not working. I have this code:

bool LoginPage::createConnection() {
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("Driver={SQL Server Native Client RDA 11.0};Server=BOGDAN;Database=StockManager;Trusted_Connection=yes;");

    if (!db.open()) {
        QMessageBox::warning(this, "Eroare", "Connection failed!");
        qDebug() << "Detailed error: " << db.lastError().text();
        return false;
    }
    else {
        QMessageBox::information(this, "OK", "You're connected!");
        qDebug() << "Successfully connected!";
        return true;
    }
}

But every time I run it, I got that QMessageBox::warning(this, "Eroare", "Connection failed!");

I searched on the internet different ways to connect to the database, but I understand that this is the only way if I'm using QtSql. Some issues were about the ODBC drivers that could not be installed, but I checked and the ODBC SQL Server Native Client RDA 11.0 it's installed.

Even I tried to see if the database it's not running, but I tried to run a query in the SQL Server Object Explorer from Visual Studio to check it and it's fine:

select * from Users
1   admin admin admin   admin   Admin   1
2   Ana Ionescu anaionescu  anaionescu  Normal  2

I found that connection string too, but didn't find a way to use it:

Data Source=BOGDAN;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False

I tried to connect to the database in different ways, but none of them helped me solve the problem.

0

There are 0 answers