![]() | หลายคอนโทลใช้ข้อมูลจาก northwind | ![]() |
|
ตารางข้อมูลในฐานข้อมูล NorthWind
ประกอบด้วย 8 ตาราง คือ Categories, Customers, Shippers, Products, Orders, Order Details, Employees, Suppliers แหล่งอ้างอิง - Office97 : northwind.htm - Download : northwind.mdb (532 KB) - Windows Form : nwindnet1_form.zip |
คำแนะนำ |
|
อธิบายโปรแกรม
- nwindnet1_Load() เรียกฟังก์ชัน read_mdb มาทำงาน - read_mdb() จะอ่านตาราง Categories แล้วส่ง 2 เขตข้อมูลเข้าไปใน ListBox1 - ListBox1_SelectedIndexChanged() จะอ่านตาราง Products แล้วส่ง 2 เขตข้อมูลเข้าไปใน ComboBox1 - ComboBox1_SelectedIndexChanged() จะอ่านตาราง Order Details แล้วส่ง 3 เขตข้อมูลเข้าไปใน ComboBox2 - ComboBox2_SelectedIndexChanged() จะอ่านตาราง Orders แล้วส่ง 4 เขตข้อมูลเข้าไปใน ComboBox3 - ComboBox3_SelectedIndexChanged() จะอ่านตาราง employees แล้วส่ง 2 เขตข้อมูลเข้าไปใน Datagridview1 |
Imports system.data.oledb
Public Class nwindnet1
Dim strdb As String = "c:\northwind.mdb"
Dim strconn As String = "provider=microsoft.jet.oledb.4.0;data source=" & strdb
Dim strsql As String
Dim cn As New System.Data.OleDb.OleDbConnection(strconn)
Dim cm As New OleDbCommand(strsql, cn)
Dim dr As OleDbDataReader
Private Sub nwindnet1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
read_mdb()
End Sub
Sub read_mdb()
ListBox1.Items.Clear()
strsql = "select categoryid,categoryname from categories"
cm = New OleDbCommand(strsql, cn)
cn.Open()
dr = cm.ExecuteReader()
While dr.Read()
ListBox1.Items.Add(dr.Item("categoryid") & "," & dr.Item(1))
End While
cn.Close()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ComboBox1.Items.Clear()
strsql = "select * from products where categoryid=" & Split(ListBox1.Text, ",")(0)
cm = New OleDbCommand(strsql, cn)
cn.Open()
dr = cm.ExecuteReader()
While dr.Read()
ComboBox1.Items.Add(dr.Item(0) & "," & dr.Item(1))
End While
cn.Close()
ComboBox1.Select()
ComboBox1.Text = ComboBox1.Items.Item(0)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.Items.Clear()
strsql = "select * from [order details] "
strsql &= "where productid = " & Split(ComboBox1.Text, ",")(0)
cm = New OleDbCommand(strsql, cn)
cn.Open()
dr = cm.ExecuteReader()
While dr.Read()
ComboBox2.Items.Add(dr.Item(0) &","& dr.Item(1) &","& dr.Item(3))
End While
cn.Close()
ComboBox2.Select()
ComboBox2.Text = ComboBox2.Items.Item(0)
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
ComboBox3.Items.Clear()
strsql = "select * from [orders] "
strsql &= "where orderid = " & Split(ComboBox2.Text, ",")(0)
cm = New OleDbCommand(strsql, cn)
cn.Open()
dr = cm.ExecuteReader()
While dr.Read()
ComboBox3.Items.Add(dr.Item(0) &","& dr.Item(1) &","& dr.Item(2) &","& dr.Item(3))
End While
cn.Close()
ComboBox3.Select()
ComboBox3.Text = ComboBox3.Items.Item(0)
End Sub
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
Dim x As Integer = DataGridView1.Columns.Count()
Dim i As Integer = 0
For i = 0 To x : DataGridView1.Columns.Clear() : Next
strsql = "select * from employees "
strsql &= "where employeeid = " & Split(ComboBox3.Text, ",")(2)
cm = New OleDbCommand(strsql, cn)
cn.Open()
dr = cm.ExecuteReader()
If dr.Read() Then
x = DataGridView1.Rows.Count()
DataGridView1.Columns.Add("eid", "eid")
DataGridView1.Columns.Add("ename", "ename")
DataGridView1.Rows.Add()
DataGridView1.Rows(x).Cells(0).Value = dr.Item(0)
DataGridView1.Rows(x).Cells(1).Value = dr.Item(1)
End If
cn.Close()
End Sub
End Class
|