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[])
{
QString serverName
= "LOCALHOST\\SQLEXPRESS";
db.setConnectOptions();
QString dsn
= QString("DRIVER={SQL Server Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").
arg(serverName
).
arg(dbName
);
db.setDatabaseName(dsn);
if(db.open())
{
qDebug()<<"Opened!";
}
return a.exec();
}
#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=Yes;").arg(serverName).arg(dbName);
db.setDatabaseName(dsn);
if(db.open())
{
qDebug()<<"Opened!";
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
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
#-------------------------------------------------
#
# 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
To copy to clipboard, switch view to plain text mode
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 ...
Bookmarks