Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Problem with connecting to a DataBase
Forum Update on Monday, May 27th 2025

[SOLVED]Problem with connecting to a DataBase

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarimanN2
    wrote on 27 Jul 2014, 08:04 last edited by
    #1

    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 = "127.0.0.1";
    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");
    db.setConnectOptions();
    db.setDatabaseName(dsName);
    
    if (db.open())
    {
        qDebug() << "Opened!";
        db.close();
    }
    
    else
    {
        qDebug() << "Error : " << db.lastError().text();
      
    }
    
    return a.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 27 Jul 2014, 12:15 last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NarimanN2
        wrote on 27 Jul 2014, 12:38 last edited by
        #3

        Yes I have a Database called test on SQL Server!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 28 Jul 2014, 03:26 last edited by
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hskoglund
            wrote on 28 Jul 2014, 06:41 last edited by
            #5

            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);
            @

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NarimanN2
              wrote on 28 Jul 2014, 22:01 last edited by
              #6

              changing dsName to that worked! thanks!

              1 Reply Last reply
              0

              1/6

              27 Jul 2014, 08:04

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved