Use c# to get the table names and contents of all tables in access

 

I have checked online before, and it seems that it can also be obtained by reading the access system table, but I really can’t think of what it is. I searched online again today and finally found a more convenient method. More importantly, this The method is also common to all OLEDB data sources .

 

Two methods of OleDbConnection are used here:

GetSchema

GetOleDbSchemaTable

 

Looking at the MSDN help, the writing is very unclear, so let's use the code to speak.

 

1. Obtain the architecture of OLEDB connection

 

  1. conn.Open();   
  2. DataTable cnSch = conn.GetSchema();  

 

The returned result is a DataTable, as follows:

MetaDataCollections 0 0
DataSourceInformation 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

(I can’t copy the column name, and I can’t post the picture, so I’ll just give it a try)

 

2. Then you can specify to read the contents, such as reading the information of all tables.

 

  1. DataTable tbl = conn.GetSchema("tables");   
  2. 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 certain table, such as reading the information of the mainTask table:

 

  1. DataTable colTbl = conn.GetSchema("columns"new string[] { nullnull"mainTask" });   
  2.  dgvCn.DataSource=colTbl;  

 

 

Return the partial column content of the result:

TABLE_NAME COLUMN_NAME

mainTask catalog
mainTask content
mainTask crdate
mainTask Emergency
mainTask endDate
mainTask fnrate
mainTask level
mainTask main IDs
mainTask startDate
mainTask tags
mainTask title
mainTask memo
mainTask memo2

 

4. Then you can try another method to read the table information:

 

  1. DataTable tblSch = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,  null );   
  2. DataView view = tblSch.DefaultView;   
  3. view.RowFilter = "table_type='table' or table_type='view'";   
  4.   
  5.   
  6. cmbTblList.SelectedIndexChanged -= cmbTblList_SelectedIndexChanged;   
  7. cmbTblList.DataSource = view;   
  8. cmbTblList.DisplayMember = "Table_Name";   
  9. cmbTblList.SelectedIndexChanged += cmbTblList_SelectedIndexChanged;   
  10.   
  11. dgvSch.DataSource = view;   
  12. dgv1.DataSource = tblSch;  

 

 

Return results:

detail TABLE
detail1 TABLE
mainTask TABLE
Query1 VIEW
TagAccessLog TABLE
testDetail TABLE
testLog TABLE
testMain TABLE
testTask TABLE

 

Well, almost all the information that should be retrieved is available, especially the information about the retrieved fields, which is very detailed.

Guess you like

Origin blog.csdn.net/dj1232090/article/details/3985224