0

I tried to connect to a MSSQLEXP2022 database, but it always does nothing. The database itself works fine with Windows ODBC manager and for sure from SSMS.

This is my code now, I tried several things and code snippets but nothing works.

#include "sqlconnection.h" //this file
#include <QtSql/QSqlDatabase>
#include <QtSql/QtSql>

SqlConnection::SqlConnection()
{

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); //should be fine for MSSQL?
db.setHostName("localhost\\SQLEXPRESS"); 
db.setDatabaseName("plutos");
db.setUserName("sa");
db.setPassword("plutos");

if (db.open())
{
    QSqlQuery *query = new QSqlQuery(db);
    query->exec("INSERT INTO Artikel_Stamm (Artikel_ID) VALUES (3)");

}

return;

};

In this case just happen nothing except "Writing finished!" from the return(); statement, but its a lie. In case I put the QSqlQeury above the "if" statement the return is db.open() failed (or similar), but not from the return(); statement, its seems to come from the class its self, cause without the return(); statement its the same message.

What I am doing wrong? (except wanting to learn C++)

PS: I use CMAKE and the 2 lines I add are:

...
find_package(Qt6 REQUIRED COMPONENTS Sql)
...
target_link_libraries(Plutos PRIVATE Qt${QT_VERSION_MAJOR}::Sql)
...

Before I add these lines it run in to a compile error, so I think thats right like this in CMakeLists.txt, but I don't know.

Thanks in advice

Dale K
  • 25,246
  • 15
  • 42
  • 71
T-Hope
  • 15
  • 5

1 Answers1

0

If someone has an equal Problem. It was working now with following code:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString connectString = "Driver={SQL Server};"; 
connectString.append("Server=localhost,1433;");
connectString.append("Database=plutos;"); 
connectString.append("Uid=sa;");       
connectString.append("Pwd=plutos;");       
db.setDatabaseName(connectString);
Andronicus
  • 25,419
  • 17
  • 47
  • 88
T-Hope
  • 15
  • 5