通过一个查询按钮,点击,然后取出listbox里所有的想作为条件,查询目前的代码是protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
{
string[] arr = new string[ListBox4.Items.Count];//ListBox4.Items.Count获得项数
for (int i = 0; i < ListBox4.Items.Count; i++)//循环取出每项的值
{
arr[i] = ListBox4.Items[i].Text;
}
foreach (string li in arr)//循环输出每项的值
.
.
.
}然后比如说string sql="";//如何将数组里每一个li都作为条件比如 select * from 表 where abc='li1' and abc='li2'
{
string[] arr = new string[ListBox4.Items.Count];//ListBox4.Items.Count获得项数
for (int i = 0; i < ListBox4.Items.Count; i++)//循环取出每项的值
{
arr[i] = ListBox4.Items[i].Text;
}
foreach (string li in arr)//循环输出每项的值
.
.
.
}然后比如说string sql="";//如何将数组里每一个li都作为条件比如 select * from 表 where abc='li1' and abc='li2'
解决方案 »
- listBox后台绑定的方法 到底放在那个位置
- 怎么复制表中数据到另外一个表中--高手指教
- |zyciis| 为什么在global.ascx中添加void ProcessRequest(object sender, EventArgs e)但他的HttpContext不能用
- 编程人员学习oracle分几步走呢?达到什么程度就可以了呢?基本操作会那些就行呢?感觉毫无头绪
- 请问 如何实现将用户回复内容的样式入库 并调用???
- vs2005中 asp.net 页面访问路径设置的问题
- 那位仁兄有和讯的blog和图片的源程序?
- 自定义datetime和数据库中datetime比较的问题
- 连数据库超时怎么办
- 发信问题!
- 如何查询最近6个月的数据
- for循环满足一定条件后执行某个动作,执行完毕后继续循环下半部分。求助高手帮忙。。。
我的想法是先判断数组的length,这样就可以判断出多少个and
然后根据索引去查....
但是,不知道怎么实现.
for循环里面
condition+= and abc=‘aaa’;
for外面
sql+=condition
condition+= and abc=ListBox4.Items[i].Text;
foreach (string li in arr)//循环输出每项的值
{
strSql += " and abc='"+li+"'";
}如果是你比如这个样子的话这个strSql可能就是你想要的语句...
protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
{
stringbuilder sb = new stringbuilder("select * from tablename where 1=1 ");
for (int i = 0; i < ListBox4.Items.Count; i++)//循环取出每项的值
{
sb.append(" and abc = '" + ListBox4.Items[i].Text + "'");
}
//GoSql(sb.ToString());
}
string str="";
for(int i=0;i<ListBox1.Items.Count;i++ )
{
str+=" abc='"+ ListBox1.Items[i].Text)+"' or ";
}
str=str.EndsWith("or")?str.SubString(0,str.length-2):"";
sql+="("+str+")";
string strWhere = string.Empty;
for (int i = 0; i < ListBox4.Items.Count; i++)
{
strWhere += string.Format("'{0}',", ListBox4.Items[i].Text);
}
strWhere = strWhere.Substring(0, strWhere.Length - 1);
string strSql = string.Format("select * from 表 where abc in ({0}) ", strWhere);