提问人:Kudzanai Victor 提问时间:11/8/2023 最后编辑:DaiKudzanai Victor 更新时间:11/8/2023 访问量:31
如何根据 combobox1 中的 *selected* 访问数据库表填充 combobox2,而无需为 vb.net 中的每个表编写 If 语句
How to populate combobox2 depending on *selected* access database table in combobox1 without writing If statements for each table in vb.net
问:
我想提示用户从 combobox1 中选择一个工具名称,然后自动输出,combobox2 会填充该特定工具的工具描述和大小,这是所选表中的一列。
目前我一直在使用以下代码,但是当我想更新数据库并添加另一个表时,问题就来了。这意味着还要在应用程序中添加代码。我希望应用程序能够检测到已添加新的工具表并自动显示,而无需再次编码
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' ADDING TABLE NAMES TO COMBOBOX 1 (TOOL NAME)
con.Open()
Dim schemaTable As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
For Each dr As DataRow In schemaTable.Rows
Combobox1.Items.Add(dr.Item("TABLE_NAME"))
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Combobox1.SelectedIndexChanged
'FILLING COMBOBOX 2 (TOOL DESCRIPTION AND SIZE) DEPENDING ON SELECTION IN COMBOBOX 1 (TOOL NAME)
If Combobox1.SelectedIndex = 1 Then
Combobox2.Text = ""
con.Open()
Combobox2.Items.Clear()
Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand
cmd.CommandText = "Select * from 14_POUND_HAMMER"
cmd.Connection = con
dr = cmd.ExecuteReader
While dr.Read
Combobox2.Items.Add(dr.GetString(1))
End While
dr.Close()
con.Close()
ElseIf Combobox1.SelectedIndex = 2 Then
Combobox2.Text = ""
con.Open()
Combobox2.Items.Clear()
Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand
cmd.CommandText = "Select * from 3_QUARTER_IMPACT_WRENCH"
cmd.Connection = con
dr = cmd.ExecuteReader
While dr.Read
Combobox2.Items.Add(dr.GetString(0))
End While
dr.Close()
con.Close()
ElseIf Combobox1.SelectedIndex = 3 Then
Combobox2.Text = ""
con.Open()
Combobox2.Items.Clear()
Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand
cmd.CommandText = "Select * from ABRASIVE_CUT_OFF_SAW "
cmd.Connection = con
dr = cmd.ExecuteReader
While dr.Read
Combobox2.Items.Add(dr.GetString(0))
End While
dr.Close()
con.Close()
答: 暂无答案
评论
ComboBox
Text
SELECT *
ComboBox
BindingSource
Select * from " & ComboBox1