To Documents

Code for Common ADO.NET Operations

Object Names

Variable Name Object Type
c Connection
scSelectionCommand
daDataAdapter
dsDataSet
t TextBox
tnTableName
cnColumnName
s String
n Integer
d Double
dtDataTable
tiTableIndex
drDataRow
riRowIndex
f FieldInRow
fiFieldIndex
  1. Add this Imports statement to the top of the code:
    Imports System.Data.OleDb

  2. Create a Connection object (Method 2 or 3).

    Dim c As New OleDbConnection
    c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=c:\Person.mdb"
    c.Open()

  3. Create a SelectCommand object (Method 2 or 3).

    Dim sc As OleDbCommand
    sc = c.CreateCommand()
    sc.CommandType = CommandType.Text
    sc.CommandText = "SELECT * FROM PersonData"

  4. Create a DataAdapter object (Methods 2 or 3).

    Dim da As New OleDbDataAdapter
    da.SelectCommand = sc

  5. Create and populate dataset object (Methods 2 or 3).

    Dim ds As New DataSet
    Dim n As Integer
    n = da.Fill(ds, "tn")

  6. Create data binding to textbox (Method 2).

    Dim b As New Binding("Text", ds, "tn.cn")
    t.DataBindings.Add(b)

  7. Add to Event Handlers (Method 2):

    ' Move to first row of dataset
    Me.BindingContext(ds, "tn").Position = 0

    ' Move to next row of dataset.
    Me.BindingContext(ds, "tn").Position += 1

    ' Move to previous row of dataset.
    Me.BindingContext(ds, "tn").Position -= 1

  8. Access the DataSet object directly (Method 3).

    f = ds.Tables(ti).Rows(ri).ItemArray(fi)

    or

    Dim dt As DataTable = ds.Tables(ti)
    Dim dr As DataRow = dt.Rows(ri)
    Dim f As Object = dr.ItemArray(fi)

    Object can be more specific like Integer, String, or Double.

  9. Use this code to change the SQL statement:
    Dim n As Integer
    Dim sc As OleDbCommand = c.CreateCommand()
    sc.CommandType = CommandType.Text
    sc.CommandText = newSqlString
    da.SelectCommand = sc
    ds.Clear()
    n = da.Fill(ds, "tn")
    Me.BindingContext(ds, "tn").Position = 0