簡體   English   中英

如何使用一個組合框在本地數據庫的C#中過濾另一個組合的結果

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

我花了很多時間在互聯網上尋找答案,卻一無所獲。 基本上,我有一個非常小的數據庫,總共包含5個表。 我的問題現在只處理其中兩個。 我有一個名為Model的表(是的,我知道我在命名該表時做得很糟糕。打算盡快對其重命名)。 這是Model的外觀。 型號表

Make ID是指表Makes中的唯一ID。 這是Make表的外觀。 制作桌子

我有一個使用Visual Studios 2012在C#中創建的Windows窗體應用程序。此數據庫是在該項目中創建的。 我有一個表單,其中有兩個組合框。 第一個列出了表格Makes中的信息。 它顯示了3個不同的汽車品牌。 第二個組合框顯示與其他表格不同的模型。 我可以得到第一個組合框以顯示所有品牌。 我可以得到第二個組合框來顯示所有模型。 但是我想要的是,如果他們在第一個框中選擇福特,那它只會在第二個框中顯示福特。 當他們在第一個框中選擇福特時,我需要以某種方式存儲與福特關聯的唯一ID,然后通過引用“模型”表中的“制造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。 你們讓我朝着正確的方向思考,這使我看到了我的代碼缺乏的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM