如何使用一个组合框在 C# 中使用本地数据库过滤另一个组合框的结果

How to use one combo box to filter results for another in C# with a local database

我花了很多时间在互联网上搜索答案,但一无所获。基本上我有一个非常小的数据库,总共包含 5 tables。不过,我现在的问题只涉及其中两个。我有一个 table 命名为模型(是的,我知道我在命名这个 table 方面做得不好。很快就会尝试重命名它)。这是模型的样子。 Model table

Make ID 是指table Makes 中的唯一ID。这是 table Make 的样子。 Make table

我有一个 windows 表单应用程序,它是我使用 Visual Studios 2012 在 C# 中创建的。该数据库是在该项目中创建的。我有一个表格,其中包括两个组合框。第一个列出了来自 table Makes 的信息。它显示了 3 个不同的汽车品牌。第二个组合框显示了与其他 table 不同的模型。我可以获得第一个组合框来显示所有品牌。我可以获得第二个组合框来显示所有模型。但我想要的是,如果他们 select Ford 在第一个框中,那么它只在第二个框中显示 Fords。当他们在第一个框中 select Ford 时,我需要以某种方式存储与 Ford 关联的唯一 ID,然后使用它通过引用模型 table 中的 Make ID 列来过滤第二个框。我已在 Access 中完成此操作,但无法在此处使用。这是我用来填充第一个框的代码。

 private void enterNewVehcileForm_Load(object sender, EventArgs e)
    {
        vinAutoPopulateTextBox.Text = mainMenu.VIN;

        mainMenu.connection.Open();

        SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);

        SqlCeDataReader dr = cs.ExecuteReader();
        while (dr.Read())
        {
            vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
        }

        dr.Close();
        dr.Dispose();



        mainMenu.connection.Close();
    }

这很好地填充了第一个框。我在下拉框中看到福特、雪佛兰和道奇。但是如果我在第二个盒子上尝试这个,它就不起作用了。

private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

        int num = vehicleMakeComboBox.SelectedIndex;

        mainMenu.connection.Open();

        SqlCeCommand modelSearch = new SqlCeCommand("SELECT * FROM Model WHERE [Make ID] = @num", mainMenu.connection);

        SqlCeDataReader modelRead = modelSearch.ExecuteReader();
        while (modelRead.Read())
        {
            vehicleModelComboBox.Items.Add(modelRead["Model"]);
        }

        modelRead.Close();
        modelRead.Dispose();

        mainMenu.connection.Close();

我在 while (modelRead.read()) 的行中收到一个错误,它说缺少一个参数。

任何人都可以帮助我或指出正确的方向。这是我第一次弄乱这个,所以可能我做错了。

您目前没有为 SqlCeCommand 中的参数 @num 提供值。您可以像这样为参数添加一个值:

cmd.Parameters.AddWithValue("@num", num)

在这里,您是说您在 SQL 中命名为 @num 的参数将具有变量 num 的值。

在启动 ExecuteReader 之前,将以下行添加到您的代码中。

modelSearch.Parameters.Add("num", SqlDbType.SmallInt).Value = num;

好的,我已经搞定了。我早些时候意识到我的逻辑全乱了,我可能没有很好地解释自己。我确实让它工作了,所以这里是给可能发现这个问题并遇到同样问题的任何人的。

这部分填充第一个组合框。

private void enterNewVehcileForm_Load(object sender, EventArgs e)
    {
        vinAutoPopulateTextBox.Text = mainMenu.VIN;

        mainMenu.connection.Open();

        SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);

        SqlCeDataReader dr = cs.ExecuteReader();
        while (dr.Read())
        {
            vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
        }

        dr.Close();
        dr.Dispose();           

        mainMenu.connection.Close();
    }

这部分然后根据他们在第一个框中选择的内容过滤第二个框。

        private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = null;
        string command = "SELECT * FROM Makes WHERE [Car Brand] = '" + vehicleMakeComboBox.Text + "'";
        string command2 = null;

        mainMenu.connection.Open();

        SqlCeCommand makeSearch = new SqlCeCommand(command, mainMenu.connection);

//  This part gets the ID of the car brand they picked in the first combo box.  Ford is 1, Chevy is 2, Dodge is 3  
        SqlCeDataReader makeRead = makeSearch.ExecuteReader();
        while (makeRead.Read())
        {
            ID = (makeRead["ID"].ToString());
        }

        makeRead.Close();
        makeRead.Dispose();

        vehicleModelComboBox.Items.Clear(); // Clears the combo box incase they picked a brand and then picked another

//  This part now selects all rows in the Model table that have the same value in the Make ID column as the car brand they chose in the first combo box  
        command2 = "SELECT * FROM Model WHERE [Make ID] = " + ID;

        SqlCeCommand modelSearch = new SqlCeCommand(command2, mainMenu.connection);

        SqlCeDataReader modelRead = modelSearch.ExecuteReader();
        while (modelRead.Read())
        {
            vehicleModelComboBox.Items.Add(modelRead["Model"]);
        }

        modelRead.Close();
        modelSearch.Dispose();

        mainMenu.connection.Close();
    }

感谢@TomDoesCode 和@Kami。你们让我朝着正确的方向思考,这让我看到了我的代码缺少的地方。