这种方法也可以通用所有OLEDB数据源。
这里用到了OleDbConnection两个方法:
GetSchema
GetOleDbSchemaTable
看MSDN的帮助,写的很不清楚,还是用代码来说话吧。
1、获取OLEDB连接的架构
conn.Open(); DataTable cnSch = conn.GetSchema();
返回的结果为一个DataTable,如下:
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
(列名复制不过来,图又没办法贴,就这样意思一下吧)
2、然后可以指定读取其中的内容,比如读取所有表的信息。
DataTable tbl = conn.GetSchema("tables"); dgv1.DataSource = tbl;
返回的结果如下:
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、继续,读取某张表的列信息,比如读取mainTask表的信息:
DataTable colTbl = conn.GetSchema("columns", new string[] { null, null, "mainTask" }); dgvCn.DataSource=colTbl;
返回结果部分列内容:
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、然后可以尝试用另一个方法来读取表信息:
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;
返回结果:
detail TABLE
detail1 TABLE
mainTask TABLE
Query1 VIEW
TagAccessLog TABLE
testDetail TABLE
testLog TABLE
testMain TABLE
testTask TABLE
呵呵!也是从网上转栽过来的,希望更多的人能看到,有所帮助!!!