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

喜夏-厌秋 提交于 2019-12-20 04:50:14

问题


When I run this code it is also retrieving some other fields which are not present in the table. How can I overcome this?

Dim conn As New OleDb.OleDbConnection
'Create a connection string for an Access database
Dim strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\check\a.mdb"
'Attach the connection string to the connection object
conn.ConnectionString = strConnectionString
'Open the connection
conn.Open()
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
    ComboBox1.Items.Add(TableRow.Item("COLUMN_NAME"))

The additional columns which is also retrieved are:

1.ID
2.Date create
3.Date update
4.Id
5.Lv
6.Name
7.parent Id
8.Type
9.GUID
10.Id

... and 6 more. The original schema consists of only 5 fields.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34490626/how-to-retrieve-actual-oledb-table-schema-excluding-additional-table-columns

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