Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在带有本地数据库的C#中使用一个组合框过滤另一个组合框的结果_C#_Database_Combobox_Sqlcedatareader - Fatal编程技术网

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

如何在带有本地数据库的C#中使用一个组合框过滤另一个组合框的结果,c#,database,combobox,sqlcedatareader,C#,Database,Combobox,Sqlcedatareader,我花了很长时间在互联网上搜索答案,但什么都找不到。基本上我有一个非常小的数据库,总共由5个表组成。但我现在的问题只涉及其中的两个。我有一个名为Model的表(是的,我知道我在命名这个表时做得不好。我将很快尝试重命名它)。这是模型的样子。 Make ID指表Makes中的唯一ID。这是这张桌子的样子。 我用Visual Studio 2012在C#中创建了一个windows窗体应用程序。此数据库是在该项目中创建的。我有一个表单,其中有两个组合框。第一个列表列出了表中的信息。它显示了3种不同的汽

我花了很长时间在互联网上搜索答案,但什么都找不到。基本上我有一个非常小的数据库,总共由5个表组成。但我现在的问题只涉及其中的两个。我有一个名为Model的表(是的,我知道我在命名这个表时做得不好。我将很快尝试重命名它)。这是模型的样子。

Make ID指表Makes中的唯一ID。这是这张桌子的样子。

我用Visual Studio 2012在C#中创建了一个windows窗体应用程序。此数据库是在该项目中创建的。我有一个表单,其中有两个组合框。第一个列表列出了表中的信息。它显示了3种不同的汽车品牌。第二个组合框显示与另一个表不同的型号。我可以得到第一个组合框来显示所有的品牌。我可以得到第二个组合框来显示所有的模型。但我想要的是,如果他们在第一个框中选择福特,它只会在第二个框中显示福特。当他们在第一个框中选择Ford时,我需要以某种方式存储与Ford关联的唯一ID,然后使用它通过引用Model表中的列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)
    {
        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();
    }
这会很好地填充第一个框。我在下拉框中看到福特、雪佛兰、道奇。但是如果我在第二个盒子里试试这个,它就不起作用了

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();
当(modelRead.read())时,我在
行中得到一个错误,它表示缺少一个参数


有谁能帮我或给我指出正确的方向吗。这是我第一次处理这个问题,所以可能我做错了一切。

您当前没有为
SqlCeCommand
中的参数
@num
提供值。可以为参数添加如下值:

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

这里,您的意思是,您在SQL中命名为
@num
的参数将具有变量
num
的值。您当前没有为
SqlCeCommand
中的参数
@num
提供值。可以为参数添加如下值:

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

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

的值,然后启动ExecuteReader,在代码中添加以下行

modelSearch.Parameters.Add("num", SqlDbType.SmallInt).Value = 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 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。你们让我朝着正确的方向思考,这让我看到了我的代码缺失的地方。

好吧,我让一切都正常工作了。我早些时候意识到我的逻辑完全混乱了,我可能没有很好地解释自己。我确实让它工作了,所以这里是给任何其他可能发现这一点并有相同问题的人的

此部分填充第一个组合框

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 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。你们让我朝着正确的方向思考,这让我看到了代码的不足之处。

您需要添加如下参数:modelSearch.Parameters.add(“num”,SqlDbType.SmallInt)。Value=num;您需要添加如下参数:modelSearch.Parameters.add(“num”,SqlDbType.SmallInt).Value=num;啊,好的。我试试看。非常感谢。有没有办法让变量num得到他们在第一个下拉框中选择的Make的ID值?例如,在我上面发布的表格模型中,福特的ID是1,雪佛兰是2,道奇是3。我现在的方法是存储所选汽车公司的索引,这并不是我想要的。我需要存储与他们选择的汽车品牌位于同一行的ID列中的值。这就是我在模型表中引用它的方式。我会选择与他们选择的品牌在同一索引中的ID值,然后将该值传递给num吗?@B_Martin我不太确定我是否理解,你能提出另一个问题并输入一些详细信息和代码吗?对不起,我现在看到了部分问题。我目前正在使用组合框的索引分配变量num。在我的第一个表格模型(原始问题中的链接)中,每个汽车品牌在其旁边的列中都有一个唯一的ID号。我需要在第一个下拉框中显示汽车品牌,但存储其ID值。然后我需要使用该ID值过滤第二个组合框。在Make表中(参见原始问题中的链接),有一列名为Make ID。这是Make表的外键。如果他们在第一个盒子里选福特,我只需要福特在第二个盒子里出现。啊,好的。我试试看。非常感谢。有没有办法让变量num得到他们在第一个下拉框中选择的Make的ID值?例如,在我上面发布的表格模型中,福特的ID是1,雪佛兰是2,道奇是3。我现在的方法是存储所选汽车公司的索引,这并不是我想要的。我需要存储与他们选择的汽车品牌位于同一行的ID列中的值。这就是我在模型表中引用它的方式。我会选择与他们选择的品牌在同一索引中的ID值,然后将该值传递给num吗?@B_Martin我不太确定我是否理解,你能提出另一个问题并输入一些详细信息和代码吗?对不起,我现在看到了部分问题。我是最新的