Data Access in C#

E

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
E

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 (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 said:
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)

em said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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)

em said:
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 said:
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)

em said:
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
 
E

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 said:
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)

em said:
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 said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

em said:
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 said:
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)

em said:
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.

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
 
E

em

Thanks heaps Nicholas, solved all my problems :)

Emily

Nicholas Paldino said:
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]
- (e-mail address removed)

em said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top