INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

How to retrieve and display records from an Access database using DataReader object? by PankajBanga
Posted: 16 Jun 03


This sample refers to the System.Data.OleDb Class Library namespaces and NWIND.mdb (access database).

Imports System.Data.OleDb

Replace the Form_Load event handler with the following code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'String variable to hold Connection String.
        Dim conString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                               "Data Source = C:\Program Files\Microsoft Visual Studio\VB98\NWIND.mdb"
       'Make sure you change the Data Source to the appropriate PATH.

        'Create an oleDbConnection object,
        'and then pass in the ConnectionString to the constructor.
        Dim conn As OleDbConnection = New OleDbConnection(conString)

        Dim selectString As String            'Variable to hold the SQL statement.
        Dim cmd As OleDbCommand         'Create an OleDbCommand object.
        Dim reader As OleDbDataReader   'Create an OleDbDataReader object.

        Try
            'Open the connection.
            conn.Open()

            'Initialize SQL string.
            selectString = "SELECT EmployeeID, FirstName, LastName " & _
                                 "FROM Employees "

            'Initialize OleDbCommand object.
            cmd = New OleDbCommand(selectString, conn)

            'Send the CommandText to the connection, and then build an OleDbDataReader.
            reader = cmd.ExecuteReader()

            'Loop through the records and print the values.
            While (reader.Read())
                MessageBox.Show(reader(0).ToString & " " & reader(1).ToString & " " & reader(2).ToString)
            End While

            'Close the reader and the related connection.
            reader.Close()
            conn.Close()

        Catch Excep As System.Exception
            MessageBox.Show(Excep.Message, "Access Database", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close