C#多条件查询语句

[复制链接]
查看11 | 回复5 | 2013-7-16 13:45:22 | 显示全部楼层 |阅读模式
可以写个关于where 条件发生变化的函数,给你个参考的看看,你也可以只传一个参数public void GetWhere(out string where, out string order)
{
where = "where 1=1";
int cataId = int.Parse(ddlChildren.SelectedValue);
if (cataId > 0)
where += string.Format(" and CataId={0}", cataId);
if (ddlUnitPrice.SelectedIndex > 0)
...
回复

使用道具 举报

千问 | 2013-7-16 13:45:22 | 显示全部楼层
#region 多条件搜索时,使用List集合来拼接条件(拼接Sql)StringBuilder sql = new StringBuilder("select * from PhoneNum");List wheres = new List();if (cboGroup.SelectedIndex != 0)...
回复

使用道具 举报

千问 | 2013-7-16 13:45:22 | 显示全部楼层
将sql = " select BookId,BookName,Author,DateTime,Publisher,Note from Bookwhere";改为sql = " select BookId,BookName,Author,DateTime,Publisher,Note from Bookwhere1=1";,然后在判断不为...
回复

使用道具 举报

千问 | 2013-7-16 13:45:22 | 显示全部楼层
因为你的 and 和 not 分支里,只是设置了相应的 str 的值,并没有调用ds = db.GetDataFromDB(str);所以ds保持null。在两个 str = ... 的下一行,添加 ds = db.GetDataFromDB(str);即可。NOT的语法可以是 SELECT * FROM [Tab...
回复

使用道具 举报

千问 | 2013-7-16 13:45:22 | 显示全部楼层
主要就是拼SQL语句 sql = " select BookId,BookName,Author,DateTime,Publisher,Note from Book ";str=“ where 1=1 ”;if (txtBookId.Text != "")
sql = sql +str+“ and ”+ "Boo...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行