获取CheckedListBox的已检查项值的逗号分隔值,并将其传递给查询中的Where条件 [英] Get comma separated values of checked items values of CheckedListBox and pass on to the Where condition in the query
本文介绍了获取CheckedListBox的已检查项值的逗号分隔值,并将其传递给查询中的Where条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取CheckedListBox的已检查项值的逗号分隔值并传递给Where条件sql查询
类似
< pre lang =c#> 选择 * 来自 table_name 其中 t_id IN(' 8',' 9')
解决方案
嗨Praveen,
你可以尝试这个代码
string whereCndn = string .Empty;
foreach ( var item in ChkM.SelectedItems)
{
if (item.Selected)
{
whereCndn + = item.SelectedValue + ,; // 在这里,您将获得一组选定的复选框值,如(8,9,)
}
}
string str = select * from table_name其中t_id IN( + whereCndn.Remove(whereCndn.Length - 1 )+ ); // 从字符串中删除最后一个逗号字符并将其放入字符串
DataTable dt = < span class =code-keyword> new DataTable();
dt = con。 select (str);
if (dt.Rows.Count > = 1 )
{
rptr.DataSource = dt;
rptr.DataBind();
}
希望这对你有所帮助。
问候,
RK
将Checked值存储在字符串变量中。使用TrimEnd()方法。
var selectedVal = str.TrimEnd(',');
然后在sql的where条件中使用它。
How to get comma separated values of checked items values of CheckedListBox and pass on to the Where condition sql query
like
select * from table_name where t_id IN ('8','9')
解决方案
Hi Praveen,
You could try this code
string whereCndn = string.Empty; foreach (var item in ChkM.SelectedItems) { if (item.Selected) { whereCndn += item.SelectedValue + ","; //Here you will get set of selected checkbox value like (8,9,) } } string str = "select * from table_name where t_id IN ("+ whereCndn.Remove(whereCndn.Length - 1) +")"; //Removing last comma character from the string and putting it in string DataTable dt = new DataTable(); dt = con.select(str); if (dt.Rows.Count >= 1) { rptr.DataSource = dt; rptr.DataBind(); }
Hope this helps you a bit.
Regards,
RK
Store the Checked values in a string variable . The use TrimEnd() method.
var selectedVal = str.TrimEnd(',');
Then use it in the where condition of the sql.
这篇关于获取CheckedListBox的已检查项值的逗号分隔值,并将其传递给查询中的Where条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文