Results 1 to 5 of 5

Thread: Connect MS SQL Server is Error...

  1. #1
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    56
    Qt products
    Qt4
    Platforms
    Windows

    Default Connect MS SQL Server is Error...

    I'm trying connect MS SQL server but errors :

    [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect

    This code:

    Qt Code:
    1. #include <QFile>
    2. #include <QTextStream>
    3. #include <QApplication> /* this normally is needed in the framework */
    4. #include <QMessageBox> /* do not use for command line application */
    5. #include <QOCIDriver> /* the Oracle driver */
    6. #include <QSqlDatabase> /* for setting up the connection */
    7. #include <QSqlError> /* for getting more human readable errors */
    8. #include <QSqlQuery>
    9. #include <QtDebug>
    10.  
    11. int main( int argc, char **argv )
    12. {
    13. QFile file( "ConnectString.con" );//My file connectString.con content is: localhost mydatabase sa sa
    14. if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    15. qFatal( "Could not open the file" );
    16.  
    17.  
    18.  
    19. QTextStream stream( &file );
    20. QString server ;
    21. QString database;
    22. QString username;
    23. QString pass;
    24. while( !stream.atEnd() )
    25. {
    26. stream >> server; // read from file exactly: localhost
    27. //qDebug() << server;
    28. stream >> database;
    29. // qDebug() << database;
    30. stream >> username; //read from file exactly: sa
    31. // qDebug() << username;
    32. stream >> pass; //read from file exactly: sa
    33. // qDebug() << pass;
    34.  
    35. break;
    36. }
    37.  
    38. QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    39. db.setDatabaseName("DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes");
    40. db.setDatabaseName(database);
    41. db.setPassword(pass);
    42. db.setHostName(server);
    43. db.setUserName(username);
    44. if(!db.open())
    45. {
    46. qDebug()<<db.lastError().text();
    47.  
    48.  
    49. }
    50. else
    51. {
    52. qDebug()<<"connect success!";
    53. }
    54. return 0;
    55. }
    To copy to clipboard, switch view to plain text mode 

    But I use :
    Qt Code:
    1. QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=sa;PWD=sa;Trusted_Connection=Yes");
    To copy to clipboard, switch view to plain text mode 

    It connect success

    Please help me...
    Thanks.
    Nguyá»…n LÆ°u VÅ© - Http://nguyenluuvu.com

  2. #2
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    56
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect MS SQL Server is Error...

    who can help me???
    Please...
    Nguyá»…n LÆ°u VÅ© - Http://nguyenluuvu.com

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect MS SQL Server is Error...

    First, the string built by this:
    Qt Code:
    1. "DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes"
    2. // "DRIVER={SQL Server};SERVER='localhost';DATABASE='mydatabase';UID='sa';PWD='sa';Trusted_Connection=Yes"
    To copy to clipboard, switch view to plain text mode 
    and the string you used here:
    Qt Code:
    1. "DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=sa;PWD=sa;Trusted_Connection=Yes"
    To copy to clipboard, switch view to plain text mode 
    are not the same.

    Second. If you are going to build the full connection string then why bother telling Qt the user name, password, host, and database name separately?
    I would would try this:
    Qt Code:
    1. QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("DRIVER={SQL Server};Trusted_Connection=Yes");
    3. db.setDatabaseName(database);
    4. db.setPassword(pass);
    5. db.setHostName(server);
    6. db.setUserName(username);
    7. if (db.open()) {
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    because it helps you avoid issues with quoting and maintaining horrible connection strings.

  4. The following user says thank you to ChrisW67 for this useful post:

    thaihoangluu (2nd June 2012)

  5. #4
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    56
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect MS SQL Server is Error...

    Quote Originally Posted by ChrisW67 View Post
    First, the string built by this:
    Qt Code:
    1. "DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes"
    2. // "DRIVER={SQL Server};SERVER='localhost';DATABASE='mydatabase';UID='sa';PWD='sa';Trusted_Connection=Yes"
    To copy to clipboard, switch view to plain text mode 
    and the string you used here:
    Qt Code:
    1. "DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=sa;PWD=sa;Trusted_Connection=Yes"
    To copy to clipboard, switch view to plain text mode 
    are not the same.

    Second. If you are going to build the full connection string then why bother telling Qt the user name, password, host, and database name separately?
    I would would try this:
    Qt Code:
    1. QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("DRIVER={SQL Server};Trusted_Connection=Yes");
    3. db.setDatabaseName(database);
    4. db.setPassword(pass);
    5. db.setHostName(server);
    6. db.setUserName(username);
    7. if (db.open()) {
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    because it helps you avoid issues with quoting and maintaining horrible connection strings.

    oh, thanks so much
    I solved my problem...
    Nguyá»…n LÆ°u VÅ© - Http://nguyenluuvu.com

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect MS SQL Server is Error...

    What worked in the end?

Similar Threads

  1. Connect To Sql Server With QT
    By METEOR7 in forum Qt Programming
    Replies: 6
    Last Post: 13th December 2011, 12:39
  2. program cannot connect to X server
    By Wazman in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2009, 19:28
  3. Trying to Connect To SQL Server 2005
    By rossd in forum Installation and Deployment
    Replies: 0
    Last Post: 6th March 2009, 19:56
  4. cannot connect to X server
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 14:22
  5. connect to sql server
    By raphaelf in forum Qt Programming
    Replies: 15
    Last Post: 27th February 2006, 18:06

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.