简体   繁体   中英

How can I open an access database in c#?

I'm trying to open an access database with the extension .accdb so I can read some information from this database. I have no problem doing this, if I use the wizard:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

However when try to use code to do this I always get an exception when I try to connect: 在此处输入图片说明

在此处输入图片说明

From debugging I understood that the exception comes from not having opened the connection. So I open the connection but the the database comes as empty:

在此处输入图片说明

I have looked around stack overflow and tried to apply many of the answers to similar questions but they haven't worked. In theory if I can connect through the wizard I should also be able to connect through OleDBConnection object in C#. How can I fix this?

I found the problem. My table name had a space between "Tab_Project Data" and because I didn't use [] it was looking for a table named "Tab_Project" which it couldn't find. So I just had to put "[Tab_Project Data]" inside my query and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM