By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,027 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,027 IT Pros & Developers. It's quick & easy.

Retreiving Column names from Dataset / dataAdapter

P: n/a
Need help finding simple way to retrieve a fields (or
columns) collection from a Dataset without filling it
first.
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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!
************************************************** ********************
<rm******@wfubmc.edu> wrote in message
news:0b****************************@phx.gbl...
Need help finding simple way to retrieve a fields (or
columns) collection from a Dataset without filling it
first.

Nov 20 '05 #2

P: n/a
Cor
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
Nov 20 '05 #3

P: n/a
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?
-----Original Message-----
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
.

Nov 20 '05 #4

P: n/a
Cor
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
Nov 20 '05 #5

P: n/a

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
....
...
..
Nov 20 '05 #6

P: n/a
Cor
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
Nov 20 '05 #7

P: n/a

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.
Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.