Use C # to get table names and content for all tables in Access __c#

Source: Internet
Author: User
Tags ole

Previously checked on the internet, it seems that you can also read access system tables to obtain, but really want to think about what is, and today on the Internet to find a more convenient way, and, more importantly, this method can also be common to all OLE DB data sources.

Here are two ways to use OleDbConnection:

GetSchema

GetOleDbSchemaTable

Look at MSDN Help, write is not clear, or use the code to speak it.

1. Get the schema of OLE DB connection

View plain Copy to clipboard print? Conn.    Open (); DataTable Cnsch = conn. GetSchema ();

Conn. Open (); DataTable Cnsch = conn. GetSchema ();

The result returned 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

(The name of the column can not be copied, but the picture can't be pasted.)

2, and then you can specify to read the content, such as reading all the table information.

View plain Copy to clipboard print? DataTable TBL = conn.    GetSchema ("Tables"); Dgv1. DataSource = TBL;

DataTable TBL = conn. GetSchema ("Tables"); Dgv1. DataSource = TBL;

The results returned 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 Maintask table information:

View plain Copy to clipboard print? DataTable COLTBL = conn.     GetSchema ("Columns", new string[] {null, NULL, "Maintask"}); DGVCN.DATASOURCE=COLTBL;

DataTable COLTBL = conn. GetSchema ("Columns", new string[] {null, NULL, "Maintask"}); DGVCN.DATASOURCE=COLTBL;

Returns the result part column contents:

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, and then you can try another method to read the table information:

  View plain copy to clipboard print? Datatable tblsch = conn. GetOleDbSchemaTable (Oledbschemaguid.tables, null);    dataview view = tblsch.defaultview;    view. rowfilter =  "table_type= ' table '  or table_type= ' view '";          Cmbtbllist.selectedindexchanged -= cmbtbllist_ SelectedIndexChanged;    cmbtbllist.datasource = view;    cmbtbllist.displaymember =  "table_name";    cmbtbllist.selectedindexchanged += cmbtbllist_selectedindexchanged;       dgvsch.datasource = view;    dgv1. datasource = tblsch;  

DataTable Tblsch = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables, NULL); DataView view = Tblsch.defaultview; View. RowFilter = "table_type= ' table ' or table_type= ' view '"; Cmbtbllist.selectedindexchanged-= cmbtbllist_selectedindexchanged; Cmbtbllist.datasource = view; Cmbtbllist.displaymember = "table_name"; Cmbtbllist.selectedindexchanged + = cmbtbllist_selectedindexchanged; Dgvsch.datasource = view; Dgv1. DataSource = Tblsch;

return Result:

Detail TABLE
Detail1 TABLE
Maintask TABLE
Query1 VIEW
Tagaccesslog TABLE
Testdetail TABLE
Testlog TABLE
Testmain TABLE
Testtask TABLE

Well, almost all the information should be taken, especially the information about the field, very detailed.

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.