Results 1 to 5 of 5

Thread: How to get all fields in a databasetable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    74

    How to get all fields in a databasetable

    I want to get info about the NAME OF ALL THE FIELDS in a
    access database table.
    I have opened the database with code similar to the one here.
    Someone that knows how to accomplish that?

    strSQL = "SELECT * FROM tblArtikel ORDER by TestName"

    cn = New OleDbConnection(JET_CONNECTIONSTRING)
    cn.Open()
    cm = New OleDbCommand(strSQL, cn)
    dr = cm.ExecuteReader() 'bygger en OleDb datareader

    Do While dr.Read()
    ...code to read from database
    Loop

    dr.Close()
    cn.Close()
    cm.Dispose()
    cn.Dispose()

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    It is possible to to use a DataSet and a DataAdapteer to fill the dataset with the tables schema. Then you have information about field names, constraints, primary keys etc.

    Dim ds As New DataSet
    Dim da As New OledbDataadapter(strSQL, cn)

    da.FillSchema(ds, SchemaType.Source, "tblArtikel")

    The ds.Tables("tblArtikel") hold the information you need I guess.

    (hoppas det hjälpte något =)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    74
    With
    Dim myTableArray As DataTable() = da.FillSchema(ds, schemaType.Source, "tblArtikel")

    ...for instance...

    You get an enormous structure that contains - among lots of other things - the fields in the table.

    But how to get the facts out of this structure and into for instance an array containing the fieldnames?

  4. #4
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    You can get the information like this...

    Code:
    da.FillSchema(Ds1, SchemaType.Source)
    Dim dc As DataColumn
    For Each dc In Ds1.Employees.Columns
        MsgBox(dc.ColumnName.ToString)
    Next
    This was made with a typed DataSet so it will be at bit different with a normal declared DataSet of course (Ds1.Tables("Employees".Columns etc).

    There are of course various ways to put this into an array.

    I'm not sure that this is the best solution, but it works.

  5. #5
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brisbane Australia
    Posts
    150

    datareader

    you can use .GetName to return the field name from a datareader

    eg MessageBox.Show(drd.GetName(0))
    uses an index of the field no - you can use the .fieldcount property to get the number of fields in your datareader
    hope this helps

    regards
    BH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width