Click to See Complete Forum and Search --> : How to get all fields in a databasetable
lennart
Dec 16th, 2002, 03:01 AM
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()
Athley
Dec 16th, 2002, 03:19 AM
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 =)
lennart
Dec 16th, 2002, 04:37 AM
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?
Athley
Dec 16th, 2002, 07:37 AM
You can get the information like this...
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.
briancps
Dec 16th, 2002, 03:38 PM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.