Emily,
I see what you are saying. If you want to get just the names of the
columns, then cycle through the Columns property exposed off the
DataTable, and then use the Name property on each DataColumn instance
returned.
The beauty of the DataTable is that the schema information (columns)
is kept separate from the data itself (the rows), where as in ADO
Recordset, it was just one big mess.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi Nicholas,
Sorry I was probably a bit vague, it's getting late here and I'm trying
to wrap my head around it.
Basically I've got a full datatable from a SQL source with three rows,
and four columns. The structure is thus:
Name Title Phone Email
=========================
Test1 Mr 00000001 (e-mail address removed)
Test2 Ms 00000002 (e-mail address removed)
Test3 Mrs 00000003 (e-mail address removed)
Basically I've been trying to work out some code that just loops thru
each of the rows, and only pulls out the name columns. In VB6/ADO I'd
use
For i = 1 to 3
List1.AddItem rs.Fields("Name")
rs.MoveNext
Next
I'm just wondering how to do this with C#/.Net. I know this probably
sounds pretty vague but this is my second day in .NET and I'm trying to
throw myself in the proverbial deep end just to grasp how it all works!
Thanks heaps.
"Nicholas Paldino [.NET/C# MVP]" <
[email protected]>
wrote in message Emily,
What do you mean by directly retrieve the value of a column? With
the code you are using, you are not evaluating the column name each
time through the loop. It is actually faster to get a value for a
column, using the DataColumn instance representing the column than to
use the column name.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi all,
I'm new to C# from VB6 and I'm playing around with SQL server.
I can show the value of each field in my datatable with the following
loop
...
foreach(DataRow myRow in dt.Rows)
{
foreach(DataColumn myCol in dt.Columns)
{
MessageBox.Show((myRow[myCol]).ToString());
}
}
...
but how can i directly retrieve the value of a column, i.e. "MyName"
rather than evaluating the column name each time thru the loop?
Emily