MohdRash
14th January 2012, 12:58
Hello all .. i'm a new one to qt
i needed to connect qt with a database system
so i used the following code
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString serverName = "LOCALHOST\\SQLEXPRESS";
QString dbName = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Server Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(serverName).arg(dbName);
db.setDatabaseName(dsn);
if(db.open())
{
qDebug()<<"Opened!";
}
return a.exec();
}
with following in .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2012-01-14T12:52:43
#
#-------------------------------------------------
QT += core
QT += sql
QT -= gui
TARGET = DB
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
i've passed thorught many articles and threads and unfortunately no result
some of them suggested to add sqldriver folder in my application directory it didn't work
and some of them suggested to compile the odcb driver library but i faced a compliation problem (by both GUI & command line)
any solutions please ... :confused:
i needed to connect qt with a database system
so i used the following code
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString serverName = "LOCALHOST\\SQLEXPRESS";
QString dbName = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Server Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(serverName).arg(dbName);
db.setDatabaseName(dsn);
if(db.open())
{
qDebug()<<"Opened!";
}
return a.exec();
}
with following in .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2012-01-14T12:52:43
#
#-------------------------------------------------
QT += core
QT += sql
QT -= gui
TARGET = DB
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
i've passed thorught many articles and threads and unfortunately no result
some of them suggested to add sqldriver folder in my application directory it didn't work
and some of them suggested to compile the odcb driver library but i faced a compliation problem (by both GUI & command line)
any solutions please ... :confused: