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:
#include <QFile>
#include <QTextStream>
#include <QApplication> /* this normally is needed in the framework */
#include <QMessageBox> /* do not use for command line application */
#include <QOCIDriver> /* the Oracle driver */
#include <QSqlDatabase> /* for setting up the connection */
#include <QSqlError> /* for getting more human readable errors */
#include <QSqlQuery>
#include <QtDebug>
int main( int argc, char **argv )
{
QFile file( "ConnectString.con" );
//My file connectString.con content is: localhost mydatabase sa sa qFatal( "Could not open the file" );
while( !stream.atEnd() )
{
stream >> server; // read from file exactly: localhost
//qDebug() << server;
stream >> database;
// qDebug() << database;
stream >> username; //read from file exactly: sa
// qDebug() << username;
stream >> pass; //read from file exactly: sa
// qDebug() << pass;
break;
}
db.setDatabaseName("DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes");
db.setDatabaseName(database);
db.setPassword(pass);
db.setHostName(server);
db.setUserName(username);
if(!db.open())
{
qDebug()<<db.lastError().text();
}
else
{
qDebug()<<"connect success!";
}
return 0;
}
#include <QFile>
#include <QTextStream>
#include <QApplication> /* this normally is needed in the framework */
#include <QMessageBox> /* do not use for command line application */
#include <QOCIDriver> /* the Oracle driver */
#include <QSqlDatabase> /* for setting up the connection */
#include <QSqlError> /* for getting more human readable errors */
#include <QSqlQuery>
#include <QtDebug>
int main( int argc, char **argv )
{
QFile file( "ConnectString.con" );//My file connectString.con content is: localhost mydatabase sa sa
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
qFatal( "Could not open the file" );
QTextStream stream( &file );
QString server ;
QString database;
QString username;
QString pass;
while( !stream.atEnd() )
{
stream >> server; // read from file exactly: localhost
//qDebug() << server;
stream >> database;
// qDebug() << database;
stream >> username; //read from file exactly: sa
// qDebug() << username;
stream >> pass; //read from file exactly: sa
// qDebug() << pass;
break;
}
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes");
db.setDatabaseName(database);
db.setPassword(pass);
db.setHostName(server);
db.setUserName(username);
if(!db.open())
{
qDebug()<<db.lastError().text();
}
else
{
qDebug()<<"connect success!";
}
return 0;
}
To copy to clipboard, switch view to plain text mode
But I use :
db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=sa;PWD=sa;Trusted_Connection=Yes");
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
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.
Bookmarks