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

Connecting Local MSSQL DB With QtSQL

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k 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 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

    JonBJ 1 Reply Last reply
    0
    • U UmeyrMSekban

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      U 1 Reply Last reply
      1
      • JonBJ JonB

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

        U Offline
        U Offline
        UmeyrMSekban
        wrote on 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

        piervalliP 1 Reply Last reply
        0
        • U UmeyrMSekban

          @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

          piervalliP Offline
          piervalliP Offline
          piervalli
          wrote on last edited by piervalli
          #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

          • Login

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