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.