当组合框选择数据时where子句不起作用 [英] where clause not working when combobox select data

查看:67
本文介绍了当组合框选择数据时where子句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


where子句不work.combobox无法检索数据。





工作



string Query =SELECT txIT FROM It;



不工作



string Query =SELECT txIT FROM It WHERE cmboItem ='+ cmboItem.SelectedValue +';

解决方案

< blockquote>通过连接用户输入的字符串值来构造SQL查询永远不是一个好主意。

更好地使用参数化查询。



至于你的问题:你应该先检查

 cmboItem.SelectedValue 

返回你需要的实际数据;也许

 cmboItem.SelectedText 

最适合。



但最后,当你确定这些属性中的哪一个是如果是正确的,你应该使用参数化查询:

 使用(SqlConnection con =  new  SqlConnection(yourConnectionString)){
string sql = SELECT txIT FROM It WHERE cmboItem = @value;;
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue( value,cmboItem.SelectedText);
con.Open();
// 然后执行查询,获取结果等等。
}


检查数据库中是否存在您愿意选择的项目,并手动尝试查询语句,以便确保查询生成代码正常工作正确。



结合 phil.o [ ^ ] 组合框中的where子句不起作用选择数据 [ ^ ]在他之前的解决方案中,改变了他的查询和替换:

 cmd.Parameters.AddWithValue(value,cmboItem.SelectedText); 



by(1) :

 cmd.Parameters.AddWithValue(value,cmboItem.Text); 



或(2) :

 cmd.Parameters.AddWithValue(value,cmboItem.SelectedItem.ToString()); 



见我在他的解决方案中提出的评论。

根据您填充组合框项目列表所做的数据绑定,您甚至可以通过其他方式进行。


试试这个:



  string  Query =   SELECT txIT FROM It WHERE cmboItem ='& cmboItem.SelectedText&  '; 


hi
"where" clause does not working.combobox not retrieve data.


Working

string Query ="SELECT txIT FROM It ";

Not working

string Query ="SELECT txIT FROM It WHERE cmboItem ='"+cmboItem.SelectedValue+"'";

解决方案

It is never a good idea to construct a SQL query by concatenating string values from user inputs.
Better use parameterized queries.

As to your problem: you should check first that

cmboItem.SelectedValue

returns the actual data you need ; maybe

cmboItem.SelectedText

would fit best.

But finally, when you have determined which one of these properties is the right one, you should use a parameterized query:

using (SqlConnection con = new SqlConnection(yourConnectionString)) {
   string sql = "SELECT txIT FROM It WHERE cmboItem = @value;";
   SqlCommand cmd = new SqlCommand(sql, con);
   cmd.Parameters.AddWithValue("value", cmboItem.SelectedText);
   con.Open();
   // Then execute the query, get your results, etc.
}


Check your database for the existance of the items you're willing to select and try the query sentence manually so you can ensure yourself that the query generation code works properly.

Combined with the parameterized query generation provided by phil.o[^] here where clause not working when combobox select data[^] in his previous solution, alter his query and replace:

cmd.Parameters.AddWithValue("value", cmboItem.SelectedText);


by (1):

cmd.Parameters.AddWithValue("value", cmboItem.Text);


or by (2):

cmd.Parameters.AddWithValue("value", cmboItem.SelectedItem.ToString());


See the comment I placed in his solution.
Depending on the databindings you made to populate the combobox items list you could even need to do it in some other ways.


try this:

string Query ="SELECT txIT FROM It WHERE cmboItem ='" & cmboItem.SelectedText & "'";


这篇关于当组合框选择数据时where子句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆