' To use this sample, write these lines as the very top lines of code in this class or moule.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
" Windows Form Designer generated code "
' Variables available to each method (sub or function procedure) of this class.
Private m_dtNameAgeData As System.Data.DataTable
Private m_intNameAgeDataRowIndex As Integer
Private Sub Form1_Load1(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strOleDBAccessConnString As String
Dim cnnOleDBAccessConn As OleDbConnection
Dim daOleDBAccessDataAdapter As OleDbDataAdapter
Dim dsOleDBAccessDataSet As DataSet
Me.Refresh()
' Set the connection string of the database we want to access.
strOleDBAccessConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source=C:\DatabaseName.mdb"
' Open up a connection to this database.
cnnOleDBAccessConn = New OleDb.OleDbConnection(strOleDBAccessConnString)
cnnOleDBAccessConn.Open()
' Open up a dataadapter to run an SQL statement
' against this above-opened database connection.
daOleDBAccessDataAdapter = New OleDbDataAdapter("SELECT Name,Age FROM Emp", _
cnnOleDBAccessConn)
' We can then retreive these results into a dataset object.
dsOleDBAccessDataSet = New DataSet
daOleDBAccessDataAdapter.Fill(dsOleDBAccessDataSet)
daOleDBAccessDataAdapter.Dispose()
daOleDBAccessDataAdapter = Nothing
' Notice how I've been disposing of the variables I no longer need, as I'm
' going along here? We've got our data, so now we can close our connection
' to the database.
cnnOleDBAccessConn.Close()
cnnOleDBAccessConn.Dispose()
cnnOleDBAccessConn = Nothing
' Then take the results from the 1st table of this dataset object (ok, in
' this example we've only queried 1 table, so there's only going to be 1
' in the dataset), and store them in the variable "m_dtNameAgeData".
m_dtNameAgeData = dsOleDBAccessDataSet.Tables(0)
dsOleDBAccessDataSet.Dispose()
dsOleDBAccessDataSet = Nothing
' Set the index of the row to look at, to be the 1st one in our datatable.
' note how this is starting at 0, rather than 1.
m_intNameAgeDataRowIndex = 0
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' If there is a record/row in the table following the current one, then set the
' row index to look at, to this value. Then call the below method to look at
' this row and populate the form's controls from it's values.
If Not (m_intNameAgeDataRowIndex >= m_dtNameAgeData.Rows.Count - 1) Then
m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex + 1
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' If there is a record/row in the table preceeding the current one, then set the
' row index to look at, to this value. Then call the below method to look at
' this row and populate the form's controls from it's values.
If Not (m_intNameAgeDataRowIndex <= 0) Then
m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex - 1
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End If
End Sub
Private Sub setWindowsFormControlValues(ByVal intRowIndexToUse As Integer)
' Populate the textboxes, with the details from the datatable row. This
' row to use is deciphered by the row index integer number passed into this
' sub. Note that I'm convertin the values from the datatable to string
' variable (data) types before putting them into the textboxes...
TextBox1.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Name"), String)
TextBox2.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Age"), String)
End Sub
End Class