PDA

View Full Version : Connect MS SQL Server is Error...



thaihoangluu
1st June 2012, 08:32
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
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;
}


But I use :
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=s a;PWD=sa;Trusted_Connection=Yes");

It connect success :(

Please help me...
Thanks.

thaihoangluu
2nd June 2012, 07:09
who can help me???
Please...

ChrisW67
2nd June 2012, 10:04
First, the string built by this:


"DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes"
// "DRIVER={SQL Server};SERVER='localhost';DATABASE='mydatabase';U ID='sa';PWD='sa';Trusted_Connection=Yes"
and the string you used here:

"DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=s a;PWD=sa;Trusted_Connection=Yes"

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:


QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};Trusted_Connection=Yes");
db.setDatabaseName(database);
db.setPassword(pass);
db.setHostName(server);
db.setUserName(username);
if (db.open()) {
...
}
because it helps you avoid issues with quoting and maintaining horrible connection strings.

thaihoangluu
2nd June 2012, 10:55
First, the string built by this:


"DRIVER={SQL Server};SERVER='"+server+"';DATABASE='"+database+"';UID='"+username+"';PWD='"+pass+"';Trusted_Connection=Yes"
// "DRIVER={SQL Server};SERVER='localhost';DATABASE='mydatabase';U ID='sa';PWD='sa';Trusted_Connection=Yes"
and the string you used here:

"DRIVER={SQL Server};SERVER=localhost;DATABASE=mydatabase;UID=s a;PWD=sa;Trusted_Connection=Yes"

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:


QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};Trusted_Connection=Yes");
db.setDatabaseName(database);
db.setPassword(pass);
db.setHostName(server);
db.setUserName(username);
if (db.open()) {
...
}
because it helps you avoid issues with quoting and maintaining horrible connection strings.


oh, thanks so much :D
I solved my problem...

ChrisW67
3rd June 2012, 05:07
What worked in the end?