'--------------------------------------------------------------------------- ' USING ADO IN CODE PART 1 ' Ron Kessler ' Created 12/28/04 'This demonstrates the 7 steps to connect a datagrid control to an Access Database 'We create all the objects in code instead of using the controls from the toolbox 'THERE ARE MANY WAYS TO DO THIS...I AM SHOWING YOU THE SIMPLEST WAY HERE 'MAKE SURE THE DATABASE IS IN THE \BIN FOLDER to run this demo '-------------------------------------------------------------------------- '---import the usual suspects! Imports System.data Imports System.data.OleDb Public Class frmADO Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Friend WithEvents btnExit As System.Windows.Forms.Button Private Sub InitializeComponent() Me.DataGrid1 = New System.Windows.Forms.DataGrid Me.btnExit = New System.Windows.Forms.Button CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'DataGrid1 ' Me.DataGrid1.DataMember = "" Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(0, 8) Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size(800, 184) Me.DataGrid1.TabIndex = 0 ' 'btnExit ' Me.btnExit.Location = New System.Drawing.Point(392, 216) Me.btnExit.Name = "btnExit" Me.btnExit.TabIndex = 1 Me.btnExit.Text = "Exit" ' 'frmADO ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(816, 266) Me.Controls.Add(Me.btnExit) Me.Controls.Add(Me.DataGrid1) Me.Name = "frmADO" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Using ADO Objects In Code" CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '---define all the pieces and parts IN THIS ORDER!!! Try ' STEP 1 :CONNECTION STRING Dim strConnectionString As String strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= customers.mdb" 'STEP 2: CONNECTION OBJECT(uses the strConnectionString) Dim Conn As New OleDbConnection(strConnectionString) 'STEP 3 MAKE SQL STRING(to define which records to get) Dim strSQL As String strSQL = "SELECT * from Customers" 'STEP 4 DATA ADAPTER (uses our SQL and Conn) Dim DA As New OleDbDataAdapter(strSQL, Conn) 'STEP 5 DATASET(give the ds a name that relates to the data you want to display) Dim DS As New DataSet 'STEP 6 FILL THE DATA ADAPTER DA.Fill(DS, "customers") 'STEP 7 SET DATAGRID PROPERTIES DataGrid1.DataSource = DS DataGrid1.DataMember = "customers" Catch MsgBox("I could not read the database...", MsgBoxStyle.Critical, "System Message") Me.Close() End End Try End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End End Sub End Class