|
-
Dec 16th, 2002, 04:01 AM
#1
Thread Starter
Lively Member
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()
-
Dec 16th, 2002, 04:19 AM
#2
Registered User
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 =)
-
Dec 16th, 2002, 05:37 AM
#3
Thread Starter
Lively Member
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?
-
Dec 16th, 2002, 08:37 AM
#4
Registered User
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.
-
Dec 16th, 2002, 04:38 PM
#5
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|