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

Data Access in C#

P: n/a
em
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2

P: n/a
em
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 no*@one.com
Test2 Ms 00000002 no*@one.com
Test3 Mrs 00000003 no*@one.com

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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 no*@one.com
Test2 Ms 00000002 no*@one.com
Test3 Mrs 00000003 no*@one.com

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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #4

P: n/a
em
Hi again Nicholas,

It's not the actual column name I need to get - it's the value.

If it's not _too_ much to ask could you please show me an example?

Cheers,
Emily

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od*************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 no*@one.com
Test2 Ms 00000002 no*@one.com
Test3 Mrs 00000003 no*@one.com

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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #5

P: n/a
Emily,

I think I know what the problem is. With ADO Recordsets, the recordset
maintained an internal pointer to a "current" row. This doesn't exist
anymore with the DataSet/DataTable.

Because of that, you will have to get the appropriate DataRow instance
(you can use the Find or Select method on the DataTable to get specific
rows, or use the DataView class to filter and sort your rows as well). Once
you have that, you can get a value from a column like this:

object val = dataRow["column name"];
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
Hi again Nicholas,

It's not the actual column name I need to get - it's the value.

If it's not _too_ much to ask could you please show me an example?

Cheers,
Emily

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Od*************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 no*@one.com
Test2 Ms 00000002 no*@one.com
Test3 Mrs 00000003 no*@one.com

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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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
>



Nov 17 '05 #6

P: n/a
em
Thanks heaps Nicholas, solved all my problems :)

Emily

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP09.phx.gbl...
Emily,

I think I know what the problem is. With ADO Recordsets, the recordset
maintained an internal pointer to a "current" row. This doesn't exist
anymore with the DataSet/DataTable.

Because of that, you will have to get the appropriate DataRow instance
(you can use the Find or Select method on the DataTable to get specific
rows, or use the DataView class to filter and sort your rows as well).
Once you have that, you can get a value from a column like this:

object val = dataRow["column name"];
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
Hi again Nicholas,

It's not the actual column name I need to get - it's the value.

If it's not _too_ much to ask could you please show me an example?

Cheers,
Emily

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Od*************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"em" <sd*********@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 no*@one.com
Test2 Ms 00000002 no*@one.com
Test3 Mrs 00000003 no*@one.com

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]" <mv*@spam.guard.caspershouse.com>
wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
> 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]
> - mv*@spam.guard.caspershouse.com
>
> "em" <sd*********@nowhere.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
>> 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
>>
>
>



Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.