S
S
Sardj2015-06-25 12:10:43
Qt
Sardj, 2015-06-25 12:10:43

What is wrong with the database connection?

Through the qt application I'm trying to connect to the database. Writes Data source not found, can it be programmatically specified O_o?
Here is the text of the function that connects:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("USER-PC\SQLPROTUPIN");
    db.setDatabaseName("hospital");
    db.setUserName("sa");
    db.setPassword("sasa178");
    bool ok = db.open();
    QTableWidgetItem * item = new QTableWidgetItem();
    if (!ok) {
        item->setText("Cannot open database:"+db.lastError().text());

    }else item->setText("1");
    ui->tableWidget->setItem(0,0,item);

be1ac1b6bc7644df807f2d343e48104b.jpg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sardj, 2015-06-25
@Sardj

Connected to the database like this

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","connection");
    QString path = "AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLPROTUPIN\\MSSQL\\DATA\\hospital.mdf";
    db.setDatabaseName("DRIVER={SQL Server}; Server=.\\SQLPROTUPIN;" + path + ";Database=hospital; Trusted_Connection=Yes;");
    bool ok = db.open();

Whatever happens later in the query, declare it like this QSqlQuery query(db);

V
Vitaly Filinkov, 2015-06-25
@vitfil

EMNIP, QSqlDatabase::addDatabase("QODBC3") is necessary

I
Ilya Bobkov, 2015-06-25
@heksen

As far as I remember, you need to put the database driver next to the exe. dlls. And write there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question