好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ODBCDatabaseconnenctiontoACCESSDatabase

ODBC Database connenction to ACCESS Database Hey guys, I got a little problem with the QODBC database connection. I have to open and use a ACCESS databasefile (.accdb) within my Qt programm, but I can't get it run. I think there is a mista

ODBC Database connenction to ACCESS Database

Hey guys,

I got a little problem with the QODBC database connection. I have to open and use a ACCESS databasefile (.accdb) within my Qt programm, but I can't get it run. I think there is a mistake in the database name but I am not that familiar with database handling so I need a little help with this. I started a simple mainwindow projekt and put all the database stuff in the main function. Here is the code:

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_01/13673573082180.png" alt="" >

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
#include  
#include "mainwindow.h"

#include  
#include  
#include  

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QSqlDatabase datab = QSqlDatabase::addDatabase("QODBC");

    QString databasename = "DRIVER={Microsoft Access Driver (*.accdb)};FIL={MS Access};DBQ=C://Users/Thor/Pers/Projekt Madinger/Datenbank/Daten_Hauptprogramm.accdb";

    datab.setDatabaseName(databasename);


    if(!datab.open())
    {
    datab.setConnectOptions();
    return 1;
    }
    else
    return 0;
    
    return a.exec();
} 



I can run the project without any problems but the open() funktion alway returns false. Maybe its just a noob mistake but it drives me crazy 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086275891.png" alt=":cursing:" > .

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086276822.png" alt="Go to the top of the page" > 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086277843.png" > Quote

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086278744.png" alt="" title="ThorsFeldherr is offline" > ThorsFeldherr

Beginner

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086279705.jpg" alt="" >

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086280766.png" alt=""ThorsFeldherr" is male" title=""ThorsFeldherr" is male" > 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086281657.png" alt=""ThorsFeldherr" started this thread" title=""ThorsFeldherr" started this thread" >

Posts: 3

Location: Bavaria/Germany

Occupation: Master of Engineering

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086282538.png" alt="Send private message" title="Send private message" >

2

650) this.width=650;" id="postEdit117475" src="http://cdn.verydemo.com/upload/2013_05_15/13686086283449.png" alt="" >

Friday, February 24th 2012, 5:53pm

Ok now I am a little bit further. I debugged it and I cant connect because of this error:

[Microsoft][ODBC Driver Manager] Der Datenquellenname wurde nicht gefunden, und es wurde kein Standardtreiber angegeben

in English: The data source name not found and no default driver specified

Can you help me?

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086276822.png" alt="Go to the top of the page" > 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086277843.png" > Quote

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086278744.png" alt="" title="ThorsFeldherr is offline" > ThorsFeldherr

Beginner

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086279705.jpg" alt="" >

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086280766.png" alt=""ThorsFeldherr" is male" title=""ThorsFeldherr" is male" > 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086281657.png" alt=""ThorsFeldherr" started this thread" title=""ThorsFeldherr" started this thread" >

Posts: 3

Location: Bavaria/Germany

Occupation: Master of Engineering

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/13686086282538.png" alt="Send private message" title="Send private message" >

3

650) this.width=650;" id="postEdit117476" src="http://cdn.verydemo.com/upload/2013_05_15/13686086283449.png" alt="" >

Friday, February 24th 2012, 7:38pm

Ok I got it by myself. I post the solution in case anyone else has the same problem.

I use windows 7, 32 bit. If you want to use a database from type .mdb or .accdb with ODBC you have to register the database as datasource before you can use it via C++ in any case. It is rather simple to to do:

You go into start->system
type ODBC into the search field
the result is datasource(ODBC)-> click it
Under "User-DSN" you can add a new source
you have to choose a database (which is the one you want to use) and you can give it a sourcename for example "YOURSOURCE"

After you have done that you can call the database like that:

650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_01/13673573082180.png" alt="" >

Source code

1
2
3
4
5
6
 
QSqlDatabase datab = QSqlDatabase::addDatabase("QODBC");

QString databasename = "YOURSOURCE";
datab.setDatabaseName(databasename);

datab.open(); 



Now it works! 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_05_15/136860862887219.png" alt=":thumbsup:" >

本文来源: http://www.qtforum.org/article/37487/odbc-database-connenction-to-access-database.html

查看更多关于ODBCDatabaseconnenctiontoACCESSDatabase的详细内容...

  阅读:30次