Click to See Complete Forum and Search --> : Looping through records (ADO .NET)


bewa
May 6th, 2004, 02:49 AM
I'm learning C# and I'm writing a little DB application.

I've managed to get & display the first record, but how can I browse through the other records until EOF??? something like you could use in VB:

While not rs.EOF()
rs.Edit
rs!["Column"] = "Test"
rs.Update
rs.MoveNext
Wend

any help or links to pages that would allow me to do the same as above would be great.

Current code i have:
---------------------------------------------------------------------------
myAdap = new OleDbDataAdapter ("SELECT * FROM PERSONEEL", conn);
MyCommandBuilder = new OleDbCommandBuilder( myAdap );

myAdap.Fill( ds , "Personeel");

txtName.Text = ds.Tables["Personeel"].Rows[ 0 ]["Naam" ].ToString();
txtGivenname.Text = ds.Tables["Personeel"].Rows[ 0 ]["voornaam" ].ToString();
txtDOB.Text = ds.Tables["Personeel"].Rows[ 0 ]["GeboorteDatum"].ToString();
txtAddress.Text = ds.Tables["Personeel"].Rows[ 0 ]["Adres" ].ToString();
txtSalary.Text = ds.Tables["Personeel"].Rows[ 0 ]["Salaris" ].ToString();
---------------------------------------------------------------

andy248
May 6th, 2004, 07:14 AM
I prefer the OleDbDataReader, and I do it like this:
OleDbDataReader oReader;
OleDbCommand oCmdSelect = new OleDbCommand( ... );
oReader = oCmdSelect.ExecuteReader();
while( oReader.Read() )
{
decimal myID = oReader.GetDecimal( 0 );
...
}
I've left out connection and SQL details for brevity.

bewa
May 6th, 2004, 09:17 AM
Thanks for your answer!

But OleDBDataReader doesn't allow updates as far as I know.

andy248
May 6th, 2004, 09:31 AM
That's true, for inserts and updates I use the OleDbCommand object directly:
OleDbCommand oCmdUpdate = new OleDbCommand( ... );
int nRowsUpdated = oCmdUpdate.ExecuteNonQuery();