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. connect to SQL Server Management studio
QtWS25 Last Chance

connect to SQL Server Management studio

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 499 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.
  • S Offline
    S Offline
    SeyMohsenFls
    wrote on 28 Jun 2021, 15:05 last edited by
    #1

    hello friends
    i want connect my Qt to SSMS and try this code:

        QSqlDatabase data1;
        data1 = QSqlDatabase::addDatabase("QODBC");
        data1.setConnectOptions();
        QString servername="DESKTOP-J2F5IR3\\SQLEXPRESS";
        QString dbname="NRTL_B";
    
        QString dsn = QString("DRIVER={SQL Native Client};Server=%1;Database=%2;Trusted_Connection=True;").arg(servername).arg(dbname);
        data1.setDatabaseName(dsn);
    
        if(data1.open())
        {
            qDebug() << "opened";
        }
        else
        {
            qDebug() << data1.lastError().text();
        }
    

    but it not opened and i received:

    QSqlQuery::exec: database not open
    

    how can i fix that??

    J 1 Reply Last reply 28 Jun 2021, 15:11
    0
    • S SeyMohsenFls
      28 Jun 2021, 15:05

      hello friends
      i want connect my Qt to SSMS and try this code:

          QSqlDatabase data1;
          data1 = QSqlDatabase::addDatabase("QODBC");
          data1.setConnectOptions();
          QString servername="DESKTOP-J2F5IR3\\SQLEXPRESS";
          QString dbname="NRTL_B";
      
          QString dsn = QString("DRIVER={SQL Native Client};Server=%1;Database=%2;Trusted_Connection=True;").arg(servername).arg(dbname);
          data1.setDatabaseName(dsn);
      
          if(data1.open())
          {
              qDebug() << "opened";
          }
          else
          {
              qDebug() << data1.lastError().text();
          }
      

      but it not opened and i received:

      QSqlQuery::exec: database not open
      

      how can i fix that??

      J Offline
      J Offline
      JonB
      wrote on 28 Jun 2021, 15:11 last edited by
      #2

      @SeyMohsenFls
      I don't think you mean "connect my Qt to SSMS"/"connect to SQL Server Management studio". You can't do that. SSMS is a GUI application interface to MS SQL. You are just talking about connecting to a SQL Express instance.

      QSqlQuery::exec: database not open

      You do not show any QSqlQuery. If you execute one against a non-opened database that is the error you will get.

      qDebug() << data1.lastError().text();

      I am astounded you do not say what this produces, since you know this is where the issue lies.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 28 Jun 2021, 15:44 last edited by
        #3

        @SeyMohsenFls said in connect to SQL Server Management studio:

        "DESKTOP-J2F5IR3\SQLEXPRESS"

        Apart from @JonB's observations I would guess this string is wrong since I think a server name needs a double \\ and not a single one.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        J 1 Reply Last reply 28 Jun 2021, 16:39
        0
        • C Christian Ehrlicher
          28 Jun 2021, 15:44

          @SeyMohsenFls said in connect to SQL Server Management studio:

          "DESKTOP-J2F5IR3\SQLEXPRESS"

          Apart from @JonB's observations I would guess this string is wrong since I think a server name needs a double \\ and not a single one.

          J Offline
          J Offline
          JonB
          wrote on 28 Jun 2021, 16:39 last edited by JonB
          #4

          @Christian-Ehrlicher
          Actually I don't think so. SQL Server instance names are addressed as SERVER_NAME\INSTANCE_NAME. The OP's DESKTOP-J2F5IR3\SQLEXPRESS looks good.

          I would have hoped qDebug() << data1.lastError().text() would give something other than QSqlQuery::exec: database not open, unless the OP is saying that's what he gets.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 28 Jun 2021, 23:46 last edited by
            #5

            Are you relying on Windows authentication, or should you be specifying username and password?
            Is the code running on DESKTOP-J2F5IR3 or are you trying to connect to a remote machine? SQL Express does not allow remote connections by default.
            Is the server listening on the default port?
            Is a firewall blocking access to that port?

            You could also try ".\SQLExpress", "localhost\SQLEXPRESS", or "(local)\SQLEXPRESS" if running on the same machine

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrShawn
              wrote on 30 Jun 2021, 12:17 last edited by
              #6

              If you have the instance running on a different computer you will likely have to adjust settings to accept remote connections and ensure the port is not being blocked by the firewall. Also its been my experience if you have one instance of SQL server you dont always need to specify that instance name (i.e. SQLEXPRESS is not needed).

              Are you able to connect to the instance just fine using SSMS?

              1 Reply Last reply
              0

              5/6

              28 Jun 2021, 23:46

              • Login

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