' Project SQLExamples ' Display table resulting from an SQL statement ' in DataGrid control. Imports System.Data.OleDb Public Class Form1 Inherits System.Windows.Forms.Form Private c As OleDbConnection Private sc As OleDbCommand Private da As OleDbDataAdapter Private ds As DataSet Private nRows As Integer #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 Label2 As System.Windows.Forms.Label Friend WithEvents cboSQL As System.Windows.Forms.ComboBox Friend WithEvents dgGroceries As System.Windows.Forms.DataGrid Private Sub InitializeComponent() Me.Label2 = New System.Windows.Forms.Label Me.cboSQL = New System.Windows.Forms.ComboBox Me.dgGroceries = New System.Windows.Forms.DataGrid CType(Me.dgGroceries, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(16, 16) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(104, 16) Me.Label2.TabIndex = 3 Me.Label2.Text = "SQL Statement" ' 'cboSQL ' Me.cboSQL.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cboSQL.Location = New System.Drawing.Point(136, 16) Me.cboSQL.Name = "cboSQL" Me.cboSQL.Size = New System.Drawing.Size(592, 24) Me.cboSQL.TabIndex = 8 ' 'dgGroceries ' Me.dgGroceries.DataMember = "" Me.dgGroceries.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.dgGroceries.Location = New System.Drawing.Point(24, 64) Me.dgGroceries.Name = "dgGroceries" Me.dgGroceries.Size = New System.Drawing.Size(616, 272) Me.dgGroceries.TabIndex = 9 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(752, 358) Me.Controls.Add(Me.dgGroceries) Me.Controls.Add(Me.cboSQL) Me.Controls.Add(Me.Label2) Me.Name = "Form1" Me.Text = "Groceries5 Example" CType(Me.dgGroceries, 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 ' Load SQL statements into combo box. cboSQL.Items.Add("SELECT * FROM GroceryList") cboSQL.Items.Add("SELECT ItemName, Price FROM GroceryList") cboSQL.Items.Add("SELECT ItemName, Code, Price FROM GroceryList " & _ "WHERE 2.00 < Price") cboSQL.Items.Add("SELECT ItemName, Code, Price FROM GroceryList " & _ "WHERE 2.00 < Price And Price <= 3.00") cboSQL.Items.Add("SELECT ItemName, Code, Price FROM GroceryList " & _ "WHERE 2.00 < Price And IsOnSale=True") cboSQL.Items.Add("SELECT ItemName, Code, Price, IsOnSale FROM GroceryList " & _ "WHERE ItemName = 'Chicken Wings'") ' Create and configure connection object. c = New OleDbConnection c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\Database\Groceries.mdb" ' Create and configure command object. sc = c.CreateCommand() sc.CommandType = CommandType.Text ' Create and configure data adapter object. da = New OleDbDataAdapter da.SelectCommand = sc ' Create DataSet object. ds = New DataSet End Sub Private Sub cboName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSQL.SelectedIndexChanged ' Obtain SQL statement from combo box. Dim sql As String = cboSQL.SelectedItem ' Don't forget single quotes around SQL constant string. sc.CommandText = sql ' Clear dataset. ds.Clear() ' Fill dataset from data adapter using new SQL statement. nRows = da.Fill(ds, "GroceryList") ' Bind dataset to datagrid. dgGroceries.DataSource = ds.Tables(0) End Sub End Class