Use C # To obtain the names and content of all tables in access

Source: Internet
Author: User

I checked it online before and it seems that I can also obtain it by reading the access system table, but I really want to find it on the Internet today, finally, we found a more convenient method. More importantly, this method can also be used for all oledb data sources.

 

Two oledbconnection methods are used here:

Getschema

Getoledbschematable

 

According to the help of msdn, it is not clear to write. Let's talk about it in code.

 

1. Obtain the oledb connection Architecture

Conn. open (); <br/> datatable cnsch = conn. getschema ();

The returned result is a datatable, as shown below:

Metadatacollections 0 0
Performanceinformation 0 0
Datatypes 0 0
Restrictions 0 0
Reservedwords 0 0
Columns 4 4
Indexes 5 4
Procedures 4 3
Tables 4 3
Views 3 3

(The column name cannot be copied, and the graph cannot be pasted. That's what it means)

 

2. You can specify the content to read, for example, the information of all tables.

Datatable TBL = conn. getschema ("tables"); <br/> dgv1.datasource = TBL;

 

The returned results are as follows:

Table_name table_type

Detail table
Detail1 table
Maintask table
Msysaccessstorage Access Table
Msysaccessxml Access Table
Msysaces system table
Msysnavpanegroupcategories Access Table
Msysnavpanegroups Access Table
Msysnavpanegrouptoobjects Access Table
Msysnavpaneobjectids Access Table
Msysobjects system table
Msysqueries system table
Msysrelationships system table
Query1 View
Tagaccesslog table
Testdetail table
Testlog table
Testmain table
Testtask table

 

3. Continue to read the column information of a table, such as reading the information of the maintask table:

Datatable coltbl = conn. getschema ("columns", new string [] {null, null, "maintask"}); <br/> dgvcn. datasource = coltbl;

 

The content of some returned columns:

Table_name column_name

Maintask Catalog
Maintask Content
Maintask Crdate
Maintask Emergency
Maintask Enddate
Maintask Fnrate
Maintask Level
Maintask Mainid
Maintask Startdate
Maintask Tags
Maintask Title
Maintask Memo
Maintask Memo2

 

4. Try another method to read the table information:

Datatable tblsch = Conn. getoledbschematable (oledbschemaguid. tables, null); <br/> dataview view = tblsch. defaultview; <br/> View. rowfilter = "table_type = 'table' or table_type = 'view'"; </P> <p> cmbtbllist. selectedindexchanged-= cmbtbllist_selectedindexchanged; <br/> cmbtbllist. datasource = view; <br/> cmbtbllist. displaymember = "table_name"; <br/> cmbtbllist. selectedindexchanged + = cmbtbllist_selectedindexchanged; </P> <p> dgvsch. datasource = view; <br/> dgv1.datasource = tblsch;

 

Returned results:

Detail Table
Detail1 Table
Maintask Table
Query1 View
Tagaccesslog Table
Testdetail Table
Testlog Table
Testmain Table
Testtask Table

 

Well, almost all the information to be retrieved is available, especially the field information, which is very detailed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.