473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Data Access in C#

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
6 1422
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: sffan | last post by:
I am new to database programming and was curious how others solve the problem of storing encrypted in data in db table columns and then subsequently searching for these records. The particular...
6
by: Hamed | last post by:
Hello I have employed as a developer in a software company that its team uses FoxPro / VB 6.0 / VC++ 6.0 as the developing tools and newly is going to migrate to VS.NET. There is a project...
0
by: sedefo | last post by:
I ran into this Microsoft Patterns & Practices Enterprise Library while i was researching how i can write a database independent data access layer. In my company we already use Data Access...
32
by: Neil Ginsberg | last post by:
We're using SQL Server 7 with an Access 2000 MDB as a front end with ODBC linked tables. I recently created a new set of tables for the app, and users are complaining that unsaved data is being...
1
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this...
9
by: Tony Lee | last post by:
Some time a ago, on this newsgroup the following comments were made in recommending good references for Access (2003) >I used to recommend Dr. Rick Dobson's, "Programming Access <version>" for...
3
by: Lyle Fairfield | last post by:
In a recent thread there has been discussion about Data Access Pages. It has been suggested that they are not permitted on many or most secure sites. Perhaps, that it is so, although I know of no...
1
by: Johann Blake | last post by:
I am looking for a good solution on how to implement data access in an application so that there is a clean separation between the data access layer, the business layer and the GUI layer. I am...
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.