Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looping through records in an Access Database

Posted on 2008-09-30
6
Medium Priority
?
840 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

927 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