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

前端 未结 2 977
暖寄归人
暖寄归人 2021-01-25 02:12

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
\         


        
相关标签:
2条回答
  • 2021-01-25 02:41

    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.

    0 讨论(0)
  • 2021-01-25 02:47

    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.

    0 讨论(0)
提交回复
热议问题