Important: Please read the Qt Code of Conduct -

[SOLVED]Problem with connecting to a DataBase

  • hi!it is my first time working with databases in Qt and I am trying to connect to SQL Server but I get this error :

    bq. [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect

    here is my code :

    @#include <QCoreApplication>
    #include <QtSql/QSqlDatabase>
    #include <QSqlError>
    #include <QtCore>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QString serverName = "";
    QString dbName = "test";
    QString dsName = QString("DRIVER={SQL Native Client};Server=%1;Database=%2;Trusted_Connection=True;").arg(serverName).arg(dbName);
    QSqlDatabase db  = QSqlDatabase::addDatabase("QODBC");
    if (
        qDebug() << "Opened!";
        qDebug() << "Error : " << db.lastError().text();
    return a.exec(&#41;;


  • Qt Champions 2017

    Where is the SQL Server running ? Do you have dbName called test on the SQL Server ? This error indicates that you datasource name called test not found. You will get this typical errors when the dsn name is not correct.

  • Yes I have a Database called test on SQL Server!

  • Qt Champions 2017

    Are you able to connect to you db using the same 'test' dns name ? May be you can use some existing free dbviewer to check this.

  • Hi, also try changing your dsName, like this:
    QString dsName = QString("DRIVER={SQL Server};Server=%1;Database=%2;Trusted_Connection=Yes;").arg(serverName).arg(dbName);

  • changing dsName to that worked! thanks!

Log in to reply