来自2 ComboBox的SQL查询确定第三个组合框

时间:2013-12-26 11:03:23

标签: c# sql sql-server combobox

我有3个comboBox ..

我想要第三个代码的代码,比如

SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox,我该如何进行SQL查询?

我的主要类别组合框称为mainCatU,子类别为subCatU

我设法根据subCatU生成mainCatU的值,现在我想要一个由maincategory和子类别的值确定的第三个组合框值。

它只是一个SQL查询还是其他东西?

有人可以帮忙吗?

我尝试过其他一些代码,例如

string strQuery = "SELECT * FROM Purchase where ItemID=(SELECT ItemID FROM ItemMaster where ItemName='" +  DropDownList3.SelectedItem.Text + '" and CategoryID=(SELECT CategoryID FROM ItemMaster where ItemName='"+ DropDownList3.SelectedItem.Text + '")"; 

但我不使用它..因为我这样使用它..

SqlDataAdapter daSearch = new SqlDataAdapter("SELECT companyName FROM CompanyDetail", conn);

请帮助..

1 个答案:

答案 0 :(得分:2)

背景是什么? WPF申请?

如果您的测试请求没问题,只需使用“您喜欢”的方式使用字符串:

string strQuery = "SELECT * FROM Purchase where ItemID=(SELECT ItemID FROM ItemMaster where ItemName='" +  DropDownList3.SelectedItem.Text + "' and CategoryID=(SELECT CategoryID FROM ItemMaster where ItemName='" + DropDownList3.SelectedItem.Text + "')";
SqlDataAdapter daSearch = new SqlDataAdapter(strQuery, conn);

您在之前版本中的“和”也犯了错误。

要获得更好的回复,请通过以下方式更新您的问题: - 有关整个解决方案的信息(应用类型,环境) - 有关您的数据模型的信息,因为您的请求似乎写得不好

在您发表评论后编辑1:

您的问题是您不知道如何获取comboBox中所选项的值。在这里查看SelectedItem,SelectedValue之间的差异... http://blogs.msdn.com/b/jaredpar/archive/2006/11/07/combobox-selecteditem-selectedvalue-selectedwhat.aspx?Redirected=true

准备好阅读这些值后,请使用类似

的字符串创建查询
string myRequest = "SELECT companyName FROM table where mainCategory = '" + *value of mainCatU* + "' and subcategory = '" + *value of subCatU* + "'";

然后将字符串传递给SQL并获得结果: SqlDataAdapter daSearch = new SqlDataAdapter(myRequest,conn);