Solved

Looping through records in an Access Database

Posted on 2008-09-30
6
833 Views
Last Modified: 2013-12-17
I am developing a program in C#.net with an Access backend. I am trying to access each record in the first column of table.

Example

Column1
1b
2c
3d
4e

I am trying to loop through the table to access each record 1b then 2c &.3c&4c etc.
THE TABLE HAS MULTIPLE COLUMNS
0
Comment
Question by:wademi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 85
ID: 22610411
You can simply open a recordset ... I'm not sure how to do that in C#, but in VBA:

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open "SELECT ColumnName FROM TableName", Your_Connection_Object

Do Until rst.EOF
  '/do whatever you want here
  rst.MoveNext
Loop
0
 

Author Comment

by:wademi
ID: 22610437
I am using C#.net. Not VB
0
 
LVL 85
ID: 22610558
Which is why I said: I'm not sure how to do that in C#, but in VBA
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 44

Expert Comment

by:GRayL
ID: 22611108
Create the object
  Open the Object
    For i = Start to Finish
      'Look at Field
    Next i
  Close Object
End

???

0
 
LVL 3

Accepted Solution

by:
trunghieubkit earned 500 total points
ID: 22612949
Using the Snippet below,

replacing exactly my access database file path by yours:

               string lstrFilePath = @"D:\PS_1.mdb";

replacing my table name by yours:

                string lstrQuery = "SELECT * FROM Log_Data";

And don't forget add       using System.Data.OleDb;  at first






public void ReadData()
{
    string lstrFilePath = @"D:\PS_1.mdb";
    string lstrConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lstrFilePath;                                        
    
    OleDbConnection lOledbCon = new OleDbConnection(lstrConnection);
    lOledbCon.Open();
 
    string lstrQuery = "SELECT * FROM Log_Data";
 
    OleDbCommand lOleCmd = new OleDbCommand(lstrQuery, lOledbCon);
 
 
    // Execute the DataReader and access the data.
    OleDbDataReader lOleDataReader = lOleCmd.ExecuteReader();
 
    while (lOleDataReader.Read())
    {
        Console.WriteLine("First Column's value = {0}\n", lOleDataReader[0]);
    }
 
    // Call Close when done reading.
    lOleDataReader.Close();
    lOledbCon.Close();
}

Open in new window

0
 

Author Closing Comment

by:wademi
ID: 31501786
Thanks trunghieubkit you r the best
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question