PDA

View Full Version : Connect to an existing SQL Server Database



Arshia Aghaei
16th August 2015, 08:30
Hi , i want to use an existing database from my project , but i don't know how.
I used QSqlDatabase , but no good result.
How to do it ??

anda_skoa
16th August 2015, 10:16
Show the code you have.
Nobody can help you fix a problem if nobody can look what the problem might be.

Cheers,
_

kbsk007
16th August 2015, 10:41
you can connect to database with following code:


#include "mainwindow.h"
#include <QApplication>
#include <QtSql>
#include <QMessageBox>

bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("my driver QPSQL");
db.setHostName("");
db.setDatabaseName("my testdb");
db.setUserName("");
db.setPassword("");

if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

if (!createConnection())
return 1;

MainWindow form;
form.resize(500, 600);
form.show();
return app.exec();
}