Sql connection
-
hello! i m using Qt 5.1. i want to connect my sql data with qt..but shows problem
my is here..please help me..
@
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QString>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString servername="BARUN//BARUN";
QString dbname= "testDB";QSqlDatabase db=QSqlDatabase::addDatabase("QODBC"); db.setConnectOptions(); QString dsn=QString("DRIVER{SQL NATIVE CLIENT};SERVER").arg(servername).arg(dbname); db.setDatabaseName(dsn); if(db.open()) { qDebug()<<"opened"; db.close(); } else { qDebug()<<"error"<<db.lastError().text(); } return a.exec();
}
@after compiling it shows ..enable to connect
my sql server name is=BARUN\BARUNedit: please use @ tags around your code sections; Andre
-
Hi,
Please enclose your code in coding tags, without them it's really hard to read.
Are you sure that the server name is correct ?
-
my server name is=BARUN/BARUN
CODE:
@
int main(int argc, char *argv[])
{ QCoreApplication a(argc, argv);
QString servername=“BARUN//BARUN”;
QString dbname= “testDB”;QSqlDatabase db=QSqlDatabase::addDatabase(“QODBC”); db.setConnectOptions(); QString dsn=QString(“DRIVER{SQL NATIVE CLIENT};SERVER”).arg(servername).arg(dbname); db.setDatabaseName(dsn);
if(db.open())
{ qDebug()<<“opened”; db.close(); }
else
{ qDebug()<<“error”<<db.lastError().text();
}
return a.exec();
}
@Again: use @ tags around code sections please; Andre
-
Are you sure your dsn variable contains the correct data ? Since your not using coding tags (one @ at the beginning and one at the end) I'm not sure your are not missing a %1 and a %2 to get your servername and dbname in the string.