本文介绍了如何使用同一SQL Server数据库中多个表的数据有条件地填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有2个组合框,第一个在运行时填充最多4个元素。我希望第二个组合框根据所选项目填充4个表中的1个表中的数据。 这是我的代码: Imports System.Data.SqlClient Public Class MealMenuForm Dim connection As New SqlConnection("Server = DESKTOP-1373H91; Initial Catalog = MealPreOrderSystem; Integrated Security = True") Private Sub MealMenuForm_Load(发件人为对象,e为EventArgs)处理MyBase.Load cbxType.Items.Add(" Meal") cbxType.Items.Add(" Dessert") cbxType.Items.Add(" Soft Drink") cbxType.Items.Add(" Alcoholic") 结束次 Private Sub cbxItemName_SelectedIndexChanged(sender As Object,e As EventArgs)处理cbxItemName.SelectedIndexChanged Dim mealsNames As New String("SELECT MealName FROM dbo.Dishes") Dim dessertsNames As New String("SELECT DessertName FROM dbo.Desserts") Dim sDrinksNames As New String("SELECT DrinkName FROM dbo.SoftDrinks") 将aDrinksNames调暗为新字符串("SELECT DrinkName FROM dbo.AlcoholicDrinks") connection.Open() Dim cmdMeals,cmdDesserts,cmdsDrinks,cmdaDrinks As SqlCommand cmdMeals = New SqlCommand(mealsNames,connection) cmdDesserts =新的SqlCommand(dessertsNames,连接) cmdsDrinks =新的SqlCommand(sDrinksNames,连接) cmdaDrinks =新的SqlCommand(aDrinksNames,连接) Dim readerMeals,readerDserts,readersDrinks,readeraDrinks As SqlDataReader 尝试 如果cbxType.SelectedItem =" Meal"然后是 readerMeals = cmdMeals.ExecuteReader() 虽然readerMeals.Read cbxItemName.Items.Add(readerMeals.Item(" MealName")))   结束时 connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem =" Dessert"然后是 readerDesserts = cmdDesserts.ExecuteReader() 虽然readerDesserts.Read cbxItemName.Items.Add(readerDesserts.Item(" DessertName")))   结束时 connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem =" Soft Drink"然后是 readersDrinks = cmdsDrinks.ExecuteReader 虽然readerDrinks.Read cbxItemName.Items.Add(readersDrinks.Item(" DrinkName")))$   结束时 connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem =" Alcoholic"然后是 readeraDrinks = cmdsDrinks.ExecuteReader readeraDrinks.Read cbxItemName.Items.Add(readeraDrinks.Item(" DrinkName")))$   结束时 connection.Close() connection.Dispose() connection = Nothing 结束如果 C ex ex As Exception MsgBox(ex.ToString) 最后为 connection.Close() connection.Dispose() connection = Nothing 结束尝试 结束点¥结束类解决方案 I have 2 comboboxes, where the 1st one is filled at runtime with up to 4 elements. I want the 2nd combobox to be filled with data from 1 of 4 tables based on the item chosen.Here is my code:Imports System.Data.SqlClientPublic Class MealMenuForm Dim connection As New SqlConnection("Server = DESKTOP-1373H91; Initial Catalog = MealPreOrderSystem; Integrated Security = True") Private Sub MealMenuForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load cbxType.Items.Add("Meal") cbxType.Items.Add("Dessert") cbxType.Items.Add("Soft Drink") cbxType.Items.Add("Alcoholic") End Sub Private Sub cbxItemName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxItemName.SelectedIndexChanged Dim mealsNames As New String("SELECT MealName FROM dbo.Dishes") Dim dessertsNames As New String("SELECT DessertName FROM dbo.Desserts") Dim sDrinksNames As New String("SELECT DrinkName FROM dbo.SoftDrinks") Dim aDrinksNames As New String("SELECT DrinkName FROM dbo.AlcoholicDrinks") connection.Open() Dim cmdMeals, cmdDesserts, cmdsDrinks, cmdaDrinks As SqlCommand cmdMeals = New SqlCommand(mealsNames, connection) cmdDesserts = New SqlCommand(dessertsNames, connection) cmdsDrinks = New SqlCommand(sDrinksNames, connection) cmdaDrinks = New SqlCommand(aDrinksNames, connection) Dim readerMeals, readerDesserts, readersDrinks, readeraDrinks As SqlDataReader Try If cbxType.SelectedItem = "Meal" Then readerMeals = cmdMeals.ExecuteReader() While readerMeals.Read cbxItemName.Items.Add(readerMeals.Item("MealName")) End While connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem = "Dessert" Then readerDesserts = cmdDesserts.ExecuteReader() While readerDesserts.Read cbxItemName.Items.Add(readerDesserts.Item("DessertName")) End While connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem = "Soft Drink" Then readersDrinks = cmdsDrinks.ExecuteReader While readersDrinks.Read cbxItemName.Items.Add(readersDrinks.Item("DrinkName")) End While connection.Close() connection.Dispose() connection = Nothing ElseIf cbxType.SelectedItem = "Alcoholic" Then readeraDrinks = cmdsDrinks.ExecuteReader While readeraDrinks.Read cbxItemName.Items.Add(readeraDrinks.Item("DrinkName")) End While connection.Close() connection.Dispose() connection = Nothing End If Catch ex As Exception MsgBox(ex.ToString) Finally connection.Close() connection.Dispose() connection = Nothing End Try End SubEnd Class 解决方案 这篇关于如何使用同一SQL Server数据库中多个表的数据有条件地填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 23:49