Connecting Tech Pros Worldwide Forums | Help | Site Map

Read data from database table row by row

Ester
Guest
 
Posts: n/a
#1: Nov 22 '05
I would like to read data from a database table row by row and execute some codes based on the data retrieved from each row. For example, there are 4 fields on a table called Image_Tbl. Those fields are Image ID, Image Path Name, X-Coordinate, and Y-Coordinate. Data stored on the table will be

Image ID Image Path Name X-Coordinate Y-Coordinat
ImgBtnLight image\light.gif 192px 92p
ImgBtnAC image\ac.gif 256px 152p

How should I make a loop saying before end of the database table records and for each row of data, execute the some codes. Thank you for your time

Thanks
Ester

Cor Ligthert
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Read data from database table row by row


Hi Ester,

When you are not posting to a language group it is better to tell which
language you use.

You can do this in VB
\\\
for i as integer = 0 to ds.tables(0).rowcount - 1
myfirsfield = ds.tables(0).rows(i)(0) 'the 0 can also be a name as "myX"
in both situations
'do somestuff
next
///
I hope this gives some ideas?

Cor


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Read data from database table row by row


Ester <anonymous@discussions.microsoft.com> wrote:[color=blue]
> I would like to read data from a database table row by row and
> execute some codes based on the data retrieved from each row. For
> example, there are 4 fields on a table called Image_Tbl. Those fields
> are Image ID, Image Path Name, X-Coordinate, and Y-Coordinate. Data
> stored on the table will be:
>
> Image ID Image Path Name X-Coordinate Y-Coordinate
> ImgBtnLight image\light.gif 192px 92px
> ImgBtnAC image\ac.gif 256px 152px
>
> How should I make a loop saying before end of the database table
> records and for each row of data, execute the some codes. Thank you
> for your time.[/color]

Assuming you've already loaded the table into a DataTable (eg using
DataAdapter.Fill)...

In C#:

foreach (DataRow row in table.Rows)
{
// Do something with row
}

In VB.NET:

Dim row as DataRow
For Each row in table.Rows
' Do something with row
Next

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 22 '05

re: Read data from database table row by row


Just to make the 2 versions equal

In C#:
foreach (DataRow row in table.Rows)
{
// Do something with row
}
In VB.net 2003
For Each row as DataRow in table.Rows
' Do something with row
Next



Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 22 '05

re: Read data from database table row by row


Cor Ligthert <notfirstname@planet.nl> wrote:[color=blue]
> Just to make the 2 versions equal
>
> In C#:
> foreach (DataRow row in table.Rows)
> {
> // Do something with row
> }
> In VB.net 2003
> For Each row as DataRow in table.Rows
> ' Do something with row
> Next[/color]

Cheers for that - for some reason I thought you couldn't declare a new
variable as the For Each loop variable. I'm sure I tried it the other
day and couldn't get it to work. Your sample works fine, of course,
which I'm very glad of...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread