How to retrieve actual OleDb table schema (excluding additional table columns)

点点圈 提交于 2019-12-02 03:07:44

Your problem is simply that the variable selected has the value Nothing when you insert it into the Restrictions() array. When I run the following code I only get the column names for the table named [new]:

Option Strict On

Imports System.Data.OleDb

Module Module1

    Sub Main()
        Dim connStr As String =
                "Provider=Microsoft.ACE.OLEDB.12.0;" &
                "Data Source=C:\Users\Public\test\so34490626\a.mdb"
        Using conn As New OleDbConnection(connStr)
            conn.Open()
            Dim selected As String = "new"
            Dim Restrictions() As String = {Nothing, Nothing, selected, Nothing}
            Dim CollectionName As String = "Columns"
            Dim dt As DataTable = conn.GetSchema(CollectionName, Restrictions)
            For Each TableRow As DataRow In dt.Rows
                Console.WriteLine(TableRow.Item("COLUMN_NAME"))
            Next
        End Using
    End Sub

End Module

The result is:

GENDER
MEMBER OF RISHI PRASAD
NAME OF SADHAK
PROFESSION

However, when the value of selected is Nothing ...

Dim selected As String = Nothing

... I get the "extra" columns you describe

DateCreate
DateUpdate
Id
Lv
Name
ParentId
Type
Attributes
DataType
FieldName
IndexType
SkipColumn
SpecID
Start
Width
DateDelim
DateFourDigitYear
DateLeadingZeros
DateOrder
DecimalPoint
FieldSeparator
FileType
SpecID
SpecName
SpecType
StartRow
TextDelim
TimeDelim
GENDER
MEMBER OF RISHI PRASAD
NAME OF SADHAK
PROFESSION

The reason is revealed if I change the Console.WriteLine to include the table names:

Console.WriteLine("[{0}].[{1}]", TableRow.Item("TABLE_NAME"), TableRow.Item("COLUMN_NAME"))

Then we see:

[MSysAccessStorage].[DateCreate]
[MSysAccessStorage].[DateUpdate]
[MSysAccessStorage].[Id]
[MSysAccessStorage].[Lv]
[MSysAccessStorage].[Name]
[MSysAccessStorage].[ParentId]
[MSysAccessStorage].[Type]
[MSysIMEXColumns].[Attributes]
[MSysIMEXColumns].[DataType]
[MSysIMEXColumns].[FieldName]
[MSysIMEXColumns].[IndexType]
[MSysIMEXColumns].[SkipColumn]
[MSysIMEXColumns].[SpecID]
[MSysIMEXColumns].[Start]
[MSysIMEXColumns].[Width]
[MSysIMEXSpecs].[DateDelim]
[MSysIMEXSpecs].[DateFourDigitYear]
[MSysIMEXSpecs].[DateLeadingZeros]
[MSysIMEXSpecs].[DateOrder]
[MSysIMEXSpecs].[DecimalPoint]
[MSysIMEXSpecs].[FieldSeparator]
[MSysIMEXSpecs].[FileType]
[MSysIMEXSpecs].[SpecID]
[MSysIMEXSpecs].[SpecName]
[MSysIMEXSpecs].[SpecType]
[MSysIMEXSpecs].[StartRow]
[MSysIMEXSpecs].[TextDelim]
[MSysIMEXSpecs].[TimeDelim]
[new].[GENDER]
[new].[MEMBER OF RISHI PRASAD]
[new].[NAME OF SADHAK]
[new].[PROFESSION]

The "MSys*" tables are system tables that are hidden by default in the Access user interface.

Before looping on your table rows, you need to identify the valid/permanent columns of your dataTable.

To do so, you should first browse the columns collection of your datatable object. By checking the properties of each one of these columns, you will be able to identify the temporary ones. I guess it might be hidden somewhere in the 'extended properties' of the DataColumn object.

In order to identify the right property, you will go through something like this (written on the fly ...):

For each tableColumn as DataColumn in dt.Columns
    Console.WriteLine(tableColumn.[propertyName].ToString())
    ...
Next

I do not know exactly which one of the properties will let you know if the column is part of the original table fields. You will have to guess and test in order to find it. Once it's identified, you then know how to select the rows to be added to your combobox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!