Retreiving Column names from Dataset / dataAdapter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Need help finding simple way to retrieve a fields (or
columns) collection from a Dataset without filling it
first.
 
You can do something like so:

Dim dt As DataTable = ds.Tables[0]
Dim colArray As String(dt.Columns.Count)

For counter = 0 to dt.Columns.Count - 1
colArray(counter) = dt.Columns[counter].ColumnName
Next

Written on the fly, so debug may be necessary (I am more confident of my C#
right now ;->).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Hi Cowboy,
I did try this
Dim ds as new dataset
Dim dt As DataTable = ds.Tables(0)
Dim colArray As String(dt.Columns.Count)
For counter = 0 to dt.Columns.Count - 1
colArray(counter) = dt.Columns(counter).ColumnName
Next

But there where no names.
Can you tell me what is the reason for it?

:-)))))
Need help finding simple way to retrieve a fields (or
columns) collection from a Dataset without filling it
first.



Cor
 
My reason is that I wish to create an sql prior to
executing the "Fill" for various tables.

The field / column names will be in a CheckListBox.

I'm 99% there by using :

Da.FillSchema(Ds, SchemaType.Source, "myTableName")
Dim X As String
X = Ds.GetXmlSchema()

....the trouble is that the .GetXmlSchema insists on putting
<?xml version="1.0" encoding="utf-16"?> into the first
line and my XmlTextReader craps out.

the "utf-16" needs to be "utf-8" and I don't know
where / how to control it?
 
Hi anonymous,

This I did want to hear, because with a total empty scheme you cannot do
anything,
maybe I can look tonight if I can help you or maybe the sample of Cowboy
fits you because you did fill the schema in advance?

If I see no answer I will look tonight or tomorrow.

Cor
 
Thanks, I seem to down to trying to get rid of the
"utf-16".

Dim x As String
pDa.FillSchema(Ds, SchemaType.Source, "Department")
x = pDs.GetXmlSchema() ' Always "utf-16" ????

'------Save to Disk
Dim osw As System.IO.StreamWriter
osw = New System.IO.StreamWriter"d:\myXML.xml")

osw.WriteLine(x)
osw.Close()
'------------------------

stop (debug) Here and fix utf-16 to utf-8......

' Read xml schema back
Dim reader As XmlTextReader
reader = New XmlTextReader("d:\myXML.xml")

Do While (reader.Read())
... reads ok only if not utf-16

loop
....
...
..
 
Hi Ricm,

I still don't see the problem.

But lets take it in a solution way.

When you have an XML dataset as a seperate XML file somewhere, you dont have
to use the streamreader, but in my eyes use dataset1.readXML(filename)

When you get it from a SQL or Access database you use OLEdb or SQL
connection, command and dataadapter.

With the dataadapter you fill the dataset

Then you have a filled dataset.

Then you can become the names with:

Dim myTable As DataTable
Dim myColumn As DataColumn
' For each table in the DataSet, print the ColumnName.
For Each myTable in dataset1.Tables
For Each myColumn in myTable.Columns
Console.WriteLine(myColumn.ColumnName)
Next
Next

After that you have your names, I dont know what you want to do then, but
you can connect to the datasource or do the databindings after that.

I hope it did help something, because fridayevening is not the best time for
me to provide some help, but I discovered I am the weekend most time off.

Cor
 
Thanks, I got your code to work by doing a Fill Schema
first.

I'm building an sql command based on the column names
(from a checkListbox). Some of the tables my app will be
using have millions of rows and can contain 50 or 60
columns, so I don't wish to FILL the dataset until AFTER
I've selected the columns and established the "WHERE"
portion of the sql command.

Since my app will use many tables, I need to collect the
column names based on which table was selected from a menu.
This enables me to add tables to the menu w/o need to know
column names for every table. (The query results are
displayed inside a DataGrid, so I'm not generating
an "Order By" in the final sql since the grid will
resequence).

Since the DataSet was empty, I was getting empty/blank
column names until I issued :

Da.FillSchema(Ds, SchemaType.Source, myTblName)

Now your code works and I can populate the checklistbox
items, build my sql string and Then Fill the DataSet with
only the Checked columns, saving time & memory.

Thanks a million!

Eliminates my need for the xmlReader/writer altogether.
 
Back
Top