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. Connecting Local MSSQL DB With QtSQL
Forum Updated to NodeBB v4.3 + New Features

Connecting Local MSSQL DB With QtSQL

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 898 Views 1 Watching
  • 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.
  • U Offline
    U Offline
    UmeyrMSekban
    wrote on 8 Mar 2023, 20:55 last edited by
    #1

    I have a local MSSQL Server on my computer and a local database located in my C drive under MSSQL. I'm trying to connect to that database using Qt Creator but I get "unable to open database file Error opening database" message.

    #include <QtSql>
    #include <QApplication>
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QString servername = "DESKTOP-05C2T53\\SEKBANSQL"; //Server name that shows up when I start MSSQL Server Management
        
        QString dbname = "my_db_name";
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //Not sure what to put between quotes here to make it work
    
        db.setConnectOptions();
    
        QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
    
        db.setDatabaseName(dsn);
    
        if(db.open()){
    
            qDebug() << "Opened";
            db.close();
        }
        else{
            qDebug() << "Error = " << db.lastError().text();
        }
    
        return a.exec();
    }
    

    I don't have QODBC on my computer and I'm just trying to connect locally

    J 1 Reply Last reply 8 Mar 2023, 21:50
    0
    • U UmeyrMSekban
      8 Mar 2023, 20:55

      I have a local MSSQL Server on my computer and a local database located in my C drive under MSSQL. I'm trying to connect to that database using Qt Creator but I get "unable to open database file Error opening database" message.

      #include <QtSql>
      #include <QApplication>
      #include <QtDebug>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QString servername = "DESKTOP-05C2T53\\SEKBANSQL"; //Server name that shows up when I start MSSQL Server Management
          
          QString dbname = "my_db_name";
      
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //Not sure what to put between quotes here to make it work
      
          db.setConnectOptions();
      
          QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
      
          db.setDatabaseName(dsn);
      
          if(db.open()){
      
              qDebug() << "Opened";
              db.close();
          }
          else{
              qDebug() << "Error = " << db.lastError().text();
          }
      
          return a.exec();
      }
      

      I don't have QODBC on my computer and I'm just trying to connect locally

      J Offline
      J Offline
      JonB
      wrote on 8 Mar 2023, 21:50 last edited by
      #2

      @UmeyrMSekban
      I assume you need QODBC for MS SQL Server, certainly it's not QSQLITE.

      U 1 Reply Last reply 9 Mar 2023, 11:06
      1
      • J JonB
        8 Mar 2023, 21:50

        @UmeyrMSekban
        I assume you need QODBC for MS SQL Server, certainly it's not QSQLITE.

        U Offline
        U Offline
        UmeyrMSekban
        wrote on 9 Mar 2023, 11:06 last edited by
        #3

        @JonB
        I've checked and saw ODBC is installed but when I change db to QODBC it fails to connect.
        I've been trying to setup ODBC to to make it work on QODBC but I don't understand what needs to be changed Screenshot_4.png Screenshot_3.png Screenshot_2.png

        P 1 Reply Last reply 9 Mar 2023, 12:36
        0
        • U UmeyrMSekban
          9 Mar 2023, 11:06

          @JonB
          I've checked and saw ODBC is installed but when I change db to QODBC it fails to connect.
          I've been trying to setup ODBC to to make it work on QODBC but I don't understand what needs to be changed Screenshot_4.png Screenshot_3.png Screenshot_2.png

          P Offline
          P Offline
          piervalli
          wrote on 9 Mar 2023, 12:36 last edited by piervalli 3 Sept 2023, 12:38
          #4

          @UmeyrMSekban
          Usually I use only a standard connection string, try with this

                  //Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
                  
                  m_database = QSqlDatabase::addDatabase("QODBC", connectionName);
          
                  QString dns = QString("Driver={SQL Server Native Client 11.0};Server=%1;Database=%4;Uid=%2;Pwd=%3;")
                          .arg(m_host,m_userName,m_password, m_nameDB);
                  qDebug() << "ODBC connection string" << dns << "driver";
                  m_database.setDatabaseName(dns);
                  stato =  m_database.open();
                  if(!stato){
                      qCritical() << m_database.lastError().text()  <<  QSqlDatabase::drivers();
                  }
          
          1 Reply Last reply
          0

          1/4

          8 Mar 2023, 20:55

          • Login

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