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.
  • M Offline
    M Offline
    M4RZB4Ni
    wrote on 3 Aug 2016, 18:03 last edited by M4RZB4Ni 8 Mar 2016, 18:07
    #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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Aug 2016, 22:47 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

      M 1 Reply Last reply 4 Aug 2016, 04:56
      0
      • S SGaist
        3 Aug 2016, 22:47

        Hi,

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

        M Offline
        M Offline
        M4RZB4Ni
        wrote on 4 Aug 2016, 04:56 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 4 Aug 2016, 05:02 last edited by
          #4

          @SGaist
          what you error caught during execution?

          M 1 Reply Last reply 4 Aug 2016, 05:12
          0
          • V Vicky Sharma
            4 Aug 2016, 05:02

            @SGaist
            what you error caught during execution?

            M Offline
            M Offline
            M4RZB4Ni
            wrote on 4 Aug 2016, 05:12 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 4 Aug 2016, 06:43 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

              M 1 Reply Last reply 4 Aug 2016, 19:31
              0
              • S SGaist
                4 Aug 2016, 06:43

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

                M Offline
                M Offline
                M4RZB4Ni
                wrote on 4 Aug 2016, 19:31 last edited by M4RZB4Ni 8 Apr 2016, 19:32
                #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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 4 Aug 2016, 19:47 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
                  • H Offline
                    H Offline
                    hskoglund
                    wrote on 4 Aug 2016, 19:57 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";

                    M 1 Reply Last reply 4 Aug 2016, 20:55
                    1
                    • H hskoglund
                      4 Aug 2016, 19:57

                      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";

                      M Offline
                      M Offline
                      M4RZB4Ni
                      wrote on 4 Aug 2016, 20:55 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
                      • H Offline
                        H Offline
                        hskoglund
                        wrote on 4 Aug 2016, 21:05 last edited by hskoglund 8 Apr 2016, 21:08
                        #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

                        M 1 Reply Last reply 5 Aug 2016, 07:24
                        2
                        • H hskoglund
                          4 Aug 2016, 21:05

                          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

                          M Offline
                          M Offline
                          M4RZB4Ni
                          wrote on 5 Aug 2016, 07:24 last edited by
                          #12

                          @hskoglund
                          Thank You So much man
                          My Problem Solved!

                          Thanks
                          M4RZB4Ni

                          1 Reply Last reply
                          0

                          9/12

                          4 Aug 2016, 19:57

                          • Login

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