ADO.NET Code

The VB.NET code below works with a Microsoft Access database that contains a table called Athletes.

In order to make the code below work, the path and name of the database needs to be included in the connection string.

The following VB.NET code illustrates how ADO.NET can be used to select data from a Microsoft Access database.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim stConString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=path to db"

        'Establish a connection to the database
        Dim conOlympicDB As OleDbConnection
        conOlympicDB = New OleDbConnection
        conOlympicDB.ConnectionString = stConString
        conOlympicDB.Open()

        'Create a command object to send the SQL SELECT command to the database
        Dim stSQL As String = "SELECT * FROM Athletes WHERE Team ='GBR'"
        Dim cmdSelectAthletes As New OleDbCommand
        cmdSelectAthletes.CommandText = stSQL
        cmdSelectAthletes.Connection = conOlympicDB

        'This can also be done with fewer lines of code
        'Dim cmdSelectAthletes As New OleDbCommand(stSQL, conOlympicDB)

        'Create a DataAdapter 
        Dim daOlympics As New OleDbDataAdapter(cmdSelectAthletes)

        'Create and fill a DataSet using the DataAdapter
        Dim dsAthletes As New DataSet
        daOlympics.Fill(dsAthletes, "Athletes")

        'Close the connection
        conOlympicDB.Close()

        'Display the data in the DataTable within the DataSet
        Dim stOut As String
        Dim t1 As DataTable = dsAthletes.Tables("Athletes")
        Dim row As DataRow
        For Each row In t1.Rows
            stOut = stOut & row(0) & " " _
                & row(1) & " " _
                & row(2) & " " _
                & row(3) & " " _
                & row(4) & vbNewLine
        Next
        MsgBox(stOut)

        'Display the data in the DataTable within the DataSet
        Dim stOut As String
        Dim t1 As DataTable = dsAthletes.Tables("Athletes")
        Dim row As DataRow
        For Each row In t1.Rows
            stOut = stOut & row(0) & " " & row(1) & " " & row(2) & " " & row(3) & " " & row(4) & vbNewLine
        Next
        MsgBox(stOut)
  
End Sub

The following VB.NET code shows how ADO.NET can be used to insert a new record into a Microsoft Access database. An appropriate connection string should be included where indicated.

    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click

        'Establish a connection to the database
        Dim conOlympicDB As New OleDbConnection("connection string here")
        conOlympicDB.Open()

        'Create a command object to send the SQL INSERT command to the database
        Dim cmdInsertAthlete As New OleDbCommand
        cmdInsertAthlete.Connection = conOlympicDB
        cmdInsertAthlete.CommandText = "INSERT INTO Athletes (AthleteNumber, FirstName, LastName)  VALUES(51,'Mo','Farah')"

        'Create a command object to send the SQL INSERT command to the database
        Dim cmdInsertAthlete As New OleDbCommand
        cmdInsertAthlete.Connection = conOlympicDB
        cmdInsertAthlete.CommandText = _
         "INSERT INTO Athletes (AthleteNumber, FirstName, LastName) VALUES(51,'Mo','Farah')"

        'Execute the SQL INSERT  command
        cmdInsertAthlete.ExecuteNonQuery()

        MsgBox("New athlete saved")

    End Sub