Solved

Looping through records in an Access Database

Posted on 2008-09-30
6
823 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 84
Comment Utility
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
Comment Utility
I am using C#.net. Not VB
0
 
LVL 84
Comment Utility
Which is why I said: I'm not sure how to do that in C#, but in VBA
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 44

Expert Comment

by:GRayL
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks trunghieubkit you r the best
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now