Read data from database table row by row

G

Guest

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
 
C

Cor Ligthert

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
 
J

Jon Skeet [C# MVP]

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

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
 
C

Cor Ligthert

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
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
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

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

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