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 sqlServer Database Problem
QtWS25 Last Chance

Connect To sqlServer Database Problem

Scheduled Pinned Locked Moved Solved General and Desktop
sqlserverdatabaseproblem
12 Posts 4 Posters 5.3k 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.
  • M4RZB4NiM Offline
    M4RZB4NiM Offline
    M4RZB4Ni
    wrote on last edited by M4RZB4Ni
    #1

    Hello,
    i Want To Connect To a database from SQL SERVER and I Wrote This Codes But
    Dose Not Worked!

    anybody can Help me Please?
    Codes:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSql>
    #include <QSqlDatabase>
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QString serverNam="localhost\\QSQLSERVER";
        QString dbname="msdb";
        QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setConnectOptions();
    QString dsn=QString("DRIVER={SQL Server};Server=%1;DATABASE=%2,Trusted_Connection=Yes;").arg(serverNam).arg(dbname);
    
    db.setDatabaseName(dsn);
    
        if(!db.open())
        {
            QMessageBox *sts=new QMessageBox;
            sts->setText("faild!");
            sts->exec();
        }
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    Sql Server Screenshot
    Note:
    I added

    QT       += sql
    
    

    code in my .Pro File

    Thanks.

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but, are you sure you have the driver QODBC availalbe.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M4RZB4NiM 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Might be a silly question but, are you sure you have the driver QODBC availalbe.

        M4RZB4NiM Offline
        M4RZB4NiM Offline
        M4RZB4Ni
        wrote on last edited by
        #3

        @SGaist
        silly is someone who is Rude Not SomeOne dont Khow!

        Thanks
        M4RZB4Ni

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vicky Sharma
          wrote on last edited by
          #4

          @SGaist
          what you error caught during execution?

          M4RZB4NiM 1 Reply Last reply
          0
          • V Vicky Sharma

            @SGaist
            what you error caught during execution?

            M4RZB4NiM Offline
            M4RZB4NiM Offline
            M4RZB4Ni
            wrote on last edited by
            #5

            @Vicky-Sharma
            Just Show my Message in IF CONDITION that show "fail"!
            and my ODBC Drivers Already Installed.

            Thanks
            M4RZB4Ni

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Rather than fail you should print the content of QSqlDatabase::lastError.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M4RZB4NiM 1 Reply Last reply
              0
              • SGaistS SGaist

                Rather than fail you should print the content of QSqlDatabase::lastError.

                M4RZB4NiM Offline
                M4RZB4NiM Offline
                M4RZB4Ni
                wrote on last edited by M4RZB4Ni
                #7

                @SGaist
                @Vicky-Sharma
                Thanks
                this is my Error
                http://8pic.ir/images/kab0zjxk7ytw9yao2rxe.png
                What i must do Exactly ?

                Thanks
                M4RZB4Ni

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  As silly as it may sound, fix the problem described by the message. Either your server is not accessible e.g. doesn't run or you can't access it because of missing credentials.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • hskoglundH Offline
                    hskoglundH Offline
                    hskoglund
                    wrote on last edited by
                    #9

                    Hi, also to add to @SGaist, don't use Named Pipes for the connection (they're kind of ancient) instead you could try something simple like:
                    QString dsn="DRIVER=SQL Server;Server=127.0.0.1; Database=Test;Trusted_Connection=Yes";

                    M4RZB4NiM 1 Reply Last reply
                    1
                    • hskoglundH hskoglund

                      Hi, also to add to @SGaist, don't use Named Pipes for the connection (they're kind of ancient) instead you could try something simple like:
                      QString dsn="DRIVER=SQL Server;Server=127.0.0.1; Database=Test;Trusted_Connection=Yes";

                      M4RZB4NiM Offline
                      M4RZB4NiM Offline
                      M4RZB4Ni
                      wrote on last edited by
                      #10

                      @hskoglund
                      Thanks
                      i Replaced that ip But Showed Me This Message
                      ERROR
                      Sql Server

                      Thanks
                      M4RZB4Ni

                      1 Reply Last reply
                      0
                      • hskoglundH Offline
                        hskoglundH Offline
                        hskoglund
                        wrote on last edited by hskoglund
                        #11

                        Aha, ok, then instead try specify your name and password:
                        QString dsn="DRIVER=SQL Server;Server=LocalHost;Database=Test;uid=Doctor Corpartion;pwd=MyPassword";

                        note: also I changed from 127.0.0.1 to LocalHost (it's the same)

                        EDIT: forgot to say: also try with username/password for your SQL Server, usually the username is sa

                        M4RZB4NiM 1 Reply Last reply
                        2
                        • hskoglundH hskoglund

                          Aha, ok, then instead try specify your name and password:
                          QString dsn="DRIVER=SQL Server;Server=LocalHost;Database=Test;uid=Doctor Corpartion;pwd=MyPassword";

                          note: also I changed from 127.0.0.1 to LocalHost (it's the same)

                          EDIT: forgot to say: also try with username/password for your SQL Server, usually the username is sa

                          M4RZB4NiM Offline
                          M4RZB4NiM Offline
                          M4RZB4Ni
                          wrote on last edited by
                          #12

                          @hskoglund
                          Thank You So much man
                          My Problem Solved!

                          Thanks
                          M4RZB4Ni

                          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