我有两个名为comboBox1和comboBox2的组合框。 cmbbox1填充了我之前手动添加的项目名称。在选择cmbbox1中的项目时,我想用从数据库中提取的相应供应商填充cmbbox2。有时候一件商品有多个供应商。在这种情况下,我想向所有供应商填写组合框2,以便用户选择供应商..
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter
Dim dr As SqlDataReader
Dim dt As New DataTable
cn.ConnectionString = ("Data Source=NIMO-HP\SQLEXPRESS;Initial Catalog=FYP_db;Integrated Security=True")
cmd.Connection = cn
cn.Open()
cmd.CommandText = "SELECT comName FROM TblSuppliers WHERE comSitem ='" & ComboBox1.Text & "'"
dr = cmd.ExecuteReader
dt.Load(dr)
ComboBox2.Text = dt
答案 0 :(得分:0)
试试这种方式
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter
Dim dt As New DataTable
cn.ConnectionString = ("Data Source=NIMO-HP\SQLEXPRESS;Initial Catalog=FYP_db;Integrated Security=True")
cn.Open()
cmd.Connection = cn
cmd.CommandText = "SELECT comName FROM TblSuppliers WHERE comSitem ='" & ComboBox1.Text & "'"
cmd.CommandType = CommandType.Text
adapter.SelectCommand = cmd
adapter.Fill(dt)
ComboBox2.DataSource = dt
ComboBox2.ValueMember = "comName"
ComboBox2.DisplayMember = "comName"