Results 1 to 16 of 16

Thread: connect to sql server

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default connect to sql server

    Hi everybody,

    QT: 4.1.1
    Compiler: MINGW 4.1
    OS: WINXP

    I am trying to connect to my local sql server, and i was not able:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=inventar;UID=rapha;PWD=raphaelf");
    4. db.setUserName("rapha");
    5. db.setPassword("raphaelf");
    6. if(!db.open())
    7. {
    8. QMessageBox::information(this, "Application name",
    9. "not connected");
    10. return false;
    11.  
    12. }
    13. else
    14. QMessageBox::information(this, "Application name",
    15. "connected");
    16. return true;
    To copy to clipboard, switch view to plain text mode 
    Postgres connection works perfect:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("DRIVER={PostgreSQL};SERVER=localhost;DATABASE=inventar;UID=rapha;PWD=raphaelf");
    4. db.setUserName("rapha");
    5. db.setPassword("raphaelf");
    6. if(!db.open())
    7. {
    8. QMessageBox::information(this, "Application name",
    9. "not connected");
    10. return false;
    11.  
    12. }
    13. else
    14. QMessageBox::information(this, "Application name",
    15. "connected");
    16. return true;
    To copy to clipboard, switch view to plain text mode 

    What need i to connect to my sql server?
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    What kind of SQL server is that?

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: connect to sql server

    Hi,
    Microsoft SQL Server 2000 Version 8.0
    Think DigitalGasoline

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    Did you try skipping that DRIVER parameter in your database name?

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: connect to sql server

    hi wisota,
    yes it not works
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("inventar");
    4. db.setUserName("rapha");
    5. db.setPassword("raphaelf");
    To copy to clipboard, switch view to plain text mode 

    with the QT example SQL Browser, i was also not able to connect using ODBC

    if i configure a "inventar.dsn" file in administrative tools (control panel winxp). I was able to connect OVER the SQL Browser Example but not from my app.
    I will have in the future my database in a server. So i must be shure that on clients are no configurations needed or required. At my office where i work i have a app running that i can connect from client to server without to configure something special. (This program was programmed with QT3.3.4.)
    I am using the same strategy to solve my problem, so i am not understanding whats wrong
    Last edited by raphaelf; 26th February 2006 at 16:53.
    Think DigitalGasoline

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    Did you make an entry in the odbc manager for your database?

  7. #7
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Cool Re: connect to sql server

    hi wysota..
    with my old program (QT3). I could connect me without to configure anything on the client and on the server like this:
    Qt Code:
    1. bool MainWindow::Verbinden()
    2. { QSqlDatabase *db = QSqlDatabase::addDatabase("QODBC3");
    3. db->setHostName("pcpsr5");
    4. db->setDatabaseName("DRIVER={SQL Server};SERVER=PCPSR5;DATABASE=inventar;UID=AdminCI;PWD=xxxxx");
    5. db->setUserName("AdminCI");
    6. db->setPassword("xxxxx");
    7. if(!db->open()){
    8. db->lastError().showMessage();
    9. return false;
    10. }
    11. return true;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Why should the same strategy not work with qt4
    Think DigitalGasoline

  8. #8
    Join Date
    Jan 2006
    Posts
    109
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: connect to sql server

    Has Qt been built with ODBC support? What is the output of QSqlDatabase::drivers()?

    The name of the driver is QODBC, not QODBC3.

  9. #9
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Re: connect to sql server

    Hi Dimitri.
    I have installed QT4.1.1 not from binary..from the exe file

    My output of QSqlDatabase::drivers() is: QODBC, QODBC3, QSQLITE

    My last Post was a example for QT3 thats why QODBC3...

    Example SQL Browser show me QSQLITE and QODBC and not QODBC3
    Last edited by raphaelf; 26th February 2006 at 20:24.
    Think DigitalGasoline

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    Do you get any messages from Qt? What exactly happens?

  11. #11
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: connect to sql server

    hi,
    with following code i ca not read the message complete, but it say something about Error on connecting user "sa". Reason: no security SQL Server connection..
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=inventar;UID=sa;PWD=raphaelf");
    4. db.setUserName("sa");
    5. db.setPassword("raphaelf");
    6. if(!db.open()){
    7. QMessageBox::information(this,"",db.lastError().text());
    8. return false;
    9. }
    10. return true;
    To copy to clipboard, switch view to plain text mode 

    If i try like following i get this error: Data Source Name not found and no default driver specified QODBC3 : Unable to connect
    Qt Code:
    1. ..
    2. db.setDatabaseName("inventar");
    3. ..
    To copy to clipboard, switch view to plain text mode 
    Last edited by raphaelf; 26th February 2006 at 22:25.
    Think DigitalGasoline

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    So the problem lies not in Qt or your application but in the database engine configuration. Try to connect with a different user, MS SQL may have non-secure connections for "sa" (system-admin) user disabled.

  13. #13
    Join Date
    Feb 2006
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect to sql server

    /************************************************** ******
    **连接sqlserver
    **
    ************************************************** *******/
    bool UpgradeWizardPage::ConnectionSqlServer(QString user,QString password,QString host,QString database)
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","QODBC1");//and connectionName

    // db= QSqlDatabase::addDatabase("QODBC","QODBC1");
    db.setDatabaseName("DRIVER={SQL Server};Server="+host+";Database="+database+";Uid= "+user+";Pwd="+password+";");
    db.setHostName(host);

    db.setUserName(user);
    db.setPassword(password);
    db.setConnectOptions("SQL_ATTR_CONNECTION_TIMEOUT= 60");

    if (!db.open()) {
    return false;
    }else{
    return true;
    }
    }


    it's worked well,why you have programe?have you installed the sqlserver's odbc driver???

  14. #14
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: connect to sql server

    hi everybody,

    I have tried at my Office to connect from client to our Server and it run Perfekt! We have MS SQL Server 7.0 installed there..hmmm..could by that ODBC doesent support the newer Version?

    Thanks for all replies

    here the code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setHostName("pcpsr5");
    3. db.setDatabaseName("DRIVER={SQL Server};SERVER=pcpsr5;DATABASE=inventar;UID=sa;PWD=xxxxx");
    4. db.setUserName("sa");
    5. db.setPassword("xxxxx");
    6. if(!db.open())
    7. {
    8. QMessageBox::information(this, "Application name",
    9. "not connected");
    10. return false;
    11.  
    12. }
    13. else
    14. QMessageBox::information(this, "Application name",
    15. "connected");
    16. return true;
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect to sql server

    It could be that in the office the server is configured differently. Try to connect to the server without Qt, using regular sql console provided with the dbms (if there is one) to see if you can connect to the server at all.

  16. #16
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: connect to sql server

    Hi Wysota!

    You are right..It was not a QT Problem. It was on my SQL Server at my Machine..
    It was a Authentification Setting. I had to choose: "SQL Server and Windows"

    Thank you very much...

    Thanks for open my mind

    This is the best forum!
    Think DigitalGasoline

Similar Threads

  1. Poor performance with Qt 4.3 and Microsoft SQL Server
    By Korgen in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 11:28
  2. Connect to a server, send data and exit
    By Pepe in forum Newbie
    Replies: 6
    Last Post: 27th July 2007, 12:29
  3. cannot connect to X server
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 15:22
  4. Replies: 1
    Last Post: 4th October 2006, 17:05
  5. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 17:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.