473,765 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(DataCol umn 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 1442
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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(DataCol umn 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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(DataCol umn 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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c om> wrote
in message news:%2******** ********@TK2MSF TNGP15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(DataCol umn 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.c om> wrote in
message news:Od******** *****@TK2MSFTNG P15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c om> wrote
in message news:%2******** ********@TK2MSF TNGP15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(DataCol umn 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.co m

"em" <sd*********@no where.com> wrote in message
news:u1******** ******@TK2MSFTN GP10.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.c om> wrote
in message news:Od******** *****@TK2MSFTNG P15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c om> wrote
in message news:%2******** ********@TK2MSF TNGP15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(DataCol umn 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.c om> wrote in
message news:%2******** *******@TK2MSFT NGP09.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.co m

"em" <sd*********@no where.com> wrote in message
news:u1******** ******@TK2MSFTN GP10.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.c om> wrote
in message news:Od******** *****@TK2MSFTNG P15.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.co m

"em" <sd*********@no where.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c om>
wrote in message news:%2******** ********@TK2MSF TNGP15.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.co m
>
> "em" <sd*********@no where.com> wrote in message
> news:%2******** ********@tk2msf tngp13.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(DataCol umn 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
5488
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 problem that I am facing is in dealing with (privacy) critical information like credit-card #s and SSNs or business critical information like sales opportunity size or revenue in the database. The requirement is that this data be stored encrypted...
6
5784
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 modified that is to upgrade a big, more than 100,000 lines DOS based program developed by FoxPro 2.x to .NET platform. The design is as previous and the implementation will be reprogrammed. As a duty in my job I should find an answer about the
0
5410
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 Application Block (DAAB) in our .Net projects. We use SqlHelper in SQL based projects, and OracleHelper in Oracle based ones. OracleHelper was not published officially by Microsoft as part of the DAAB but it was given as a helper code in a sample .Net...
32
3228
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 lost when they move to a new record. This seems to be the case when there are multiple users. When there is a single user using it, we don't seem to have that problem. It seems that we had this problem when we first converted from an MDB back end...
1
9199
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 will work for 9i and even 10g ) No one had what I needed, so I wrote it myself. I Rule. This code isn't going for efficiency, and isn't trying to be dynamic. It doesn't create the table structure in Oracle, that's up to you. (I
9
3313
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 >people moving from power user to developer, but now I suggest you browse >it, >too. It strongly emphasizes ADO, which knowledgeable Microsoft insiders no >longer recommend, and the Access ADP client to SQL Server. He writes well, >and is a good...
3
3493
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 site that has this prohibition, and I have uploaded DAPs to various sites and used them from those sites. I do not understand why any site manager would prohibit DAPs. To the best of my knowledge DAPs, as HTM files, are merely hosted on the...
1
2586
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 looking for a robust solution for a major application. Almost every developer seems to come up with a completely different solution. While many of them are not bad, I really want a very good one. My database is SQL Server 2000 and I am using Visual...
0
14422
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 be greater than 255 characters. 2. I have an access database. I link (not import) to the contents of the excel spreadsheet. In the design view in access, Column A has the data type "memo".
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.