Retrieve data from a db (Question)

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
C

Claudia Fong

Hi,


I have a form where I need to display the data of a department that
stores in a access db.

When I first load the form it will call a function name loaddata which
will get the first record from the db and then display for the user:

oleDbConnection1.Open();
OleDbDataReader objReader = objdisplay.ExecuteReader(); --> this will
select DEPT_CODE,DEPT_NAME,DEPT_ATTN FROM DEP_TABLE

if (objReader.HasRows)
{
objReader.Read();
textBox1.Text = Convert.ToString(objReader["DEPT_CODE"]);
textBox2.Text = Convert.ToString(objReader["DEPT_NAME"]);
textBox3.Text = Convert.ToString(objReader["DEPT_ATTN"]);

(...)

Then I have various button, one of them is NEXT, which I want to read
the next record from the db. I'm new in C# and in VB, I will use the
MoveNext to move to the next record, but is it the same in C#?

Can someone help me?

Cheers!

Claudi
 
Hi Claudia,

Call OleDbDataReader.Read() again for each time you want to read the next record from the query.
However, this will keep the database connection open for a 'long' time and you might want to store the result in a local DataSet/DataTable. Using OleDbDataReader.Read won't allow you to go back to a previous record either.

You can use the OleDbDataAdapter class to fill a dataset from a query to the database.



Hi,


I have a form where I need to display the data of a department that
stores in a access db.

When I first load the form it will call a function name loaddata which
will get the first record from the db and then display for the user:

oleDbConnection1.Open();
OleDbDataReader objReader = objdisplay.ExecuteReader(); --> this will
select DEPT_CODE,DEPT_NAME,DEPT_ATTN FROM DEP_TABLE

if (objReader.HasRows)
{
objReader.Read();
textBox1.Text = Convert.ToString(objReader["DEPT_CODE"]);
textBox2.Text = Convert.ToString(objReader["DEPT_NAME"]);
textBox3.Text = Convert.ToString(objReader["DEPT_ATTN"]);

(...)

Then I have various button, one of them is NEXT, which I want to read
the next record from the db. I'm new in C# and in VB, I will use the
MoveNext to move to the next record, but is it the same in C#?

Can someone help me?

Cheers!

Claudi
 
Thanks!

If I use the DataAdapter, I can call it again for example to retrieve
data from the previous record? Because I have 3 more buttons, TOP (so
the user can move to the top of the table), Bottom(move to the bottom of
the table), Next(move to the next record) and Previous (move to the
previous record).

Do you have an example of how to use the DataAdapter?

I just started learning C#...


Cheers!

Claudi
 
Claudia,

A DataReader is fine for forward-only access, in fact it's the fastest way
to do that. But if you want to navigate you'll need to use a DataAdapter.

Google on "data application block" -- that should take you to some good
downloadable samples at Microsoft's site. A similar search on "DataAdapter
example" or "DataAdapter tutorial" will probably turn up zillions of other
examples.

--Bob
 
With a DataAdapter the idea is to connect to the database once, retrieve all data and store it in a local DataSet. There will be no need to call it again to retrieve previous records as all the records will be stored in the DataSet (in a DataTable).

If you make changes to the DataTable, add new rows, delete or change rows, you can use the DataAdapter to post back all changes to the database when you are done doing changes, or when you see fit.

In addition to Bob's search phrases you can search for 'dataAdapter1.Fill' and/or 'dataAdapter1.Update'
 
Back
Top