记得前几天有个牛人写了个查询语句,现在怎么也想不起来了 好像是QQ里根据条件来查询在线会员的例子.有很多选择的条件, 可以根据你选择省份,城市,性别, 年龄来搜索符合条件的好友, 这个条件的数目的随机的, 当时有个牛人只用一条很简洁的语句就实现了 SELECT后面跟着一堆的IF, 我忘怎么写了. 那位老大帮我想想啊. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //方式1:ADO.NETpublic string Test(string a, string b, string c,string d) { string sql = "SELECT * FROM Users WHERE 1=1"; if (!string.IsNullOrEmpty(a)) { sql += " AND name='" + a + "'"; } if (!string.IsNullOrEmpty(b)) { sql += " AND age='" + b+ "'"; } if (!string.IsNullOrEmpty(c)) { sql += " AND sex='" + c + "'"; } if (!string.IsNullOrEmpty(d)) { sql += " AND address='" + d + "'"; } return sql.ToString(); } public string Test(string a, string b, string c,string d) { string sql = "SELECT * FROM Users WHERE 1=1"; if (!string.IsNullOrEmpty(a)) { sql += " AND name='" + a + "'"; } if (!string.IsNullOrEmpty(b)) { sql += " AND age='" + b+ "'"; } if (!string.IsNullOrEmpty(c)) { sql += " AND sex='" + c + "'"; } if (!string.IsNullOrEmpty(d)) { sql += " AND address='" + d + "'"; } return sql.ToString(); } //方式2:linqpublic void Test(string a, string b, string c,string d) { QueryContext query = new QueryContext(); var q = from u in query.Users select u; if (!string.IsNullOrEmpty(a)) { q = q.Where(p => p.name == a); } if (!string.IsNullOrEmpty(b)) { q = q.Where(p => p.age == b); } if (!string.IsNullOrEmpty(c)) { q = q.Where(p => p.sex == c); } if (!string.IsNullOrEmpty(d)) { q = q.Where(p => p.address == d); } q.ToList(); //上边的所有if,只有到此处才会执行 } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/q107770540/archive/2010/07/09/5724013.aspx SELECT * FROM QQTableWHERE IsOnLine=1AND ((@Name IS NULL) OR ([Name] LIKE @Name))AND ((@City IS NULL) OR ([City]=@City ))AND ....... select * from QQTable where IsOnline=1 and isnull(@Name,QQTable.Name)and isnull(@City,QQTable.City)and isnull(@Age,QQTable.Age)更多的自己加上去 Regex.Replace 如何替换括号 js脚本错误:出现了运行时间错误 缺少';' 〓如何制作类似winform的右键弹出菜单并能响应菜单项事件?同时还要获得鼠标相对于1024*768分辩率的坐标。 namespace的问题 如何实现TreeView的无闪烁 动态生成table DropDownList 怎樣才會響應事件?在線等待 关于腾讯企业邮箱API 全局变量不能在不同函数间共享? 读xml中某节点内容的问题 小小菜鸟潜水一年, 请教几个初级问题 RadioButtonList多选值存数据库的问题
//方式1:ADO.NET
public string Test(string a, string b, string c,string d)
{
string sql = "SELECT * FROM Users WHERE 1=1";
if (!string.IsNullOrEmpty(a))
{
sql += " AND name='" + a + "'";
}
if (!string.IsNullOrEmpty(b))
{
sql += " AND age='" + b+ "'";
}
if (!string.IsNullOrEmpty(c))
{
sql += " AND sex='" + c + "'";
}
if (!string.IsNullOrEmpty(d))
{
sql += " AND address='" + d + "'";
}
return sql.ToString();
}
public string Test(string a, string b, string c,string d)
{
string sql = "SELECT * FROM Users WHERE 1=1";
if (!string.IsNullOrEmpty(a))
{
sql += " AND name='" + a + "'";
}
if (!string.IsNullOrEmpty(b))
{
sql += " AND age='" + b+ "'";
}
if (!string.IsNullOrEmpty(c))
{
sql += " AND sex='" + c + "'";
}
if (!string.IsNullOrEmpty(d))
{
sql += " AND address='" + d + "'";
}
return sql.ToString();
} //方式2:linqpublic void Test(string a, string b, string c,string d)
{
QueryContext query = new QueryContext();
var q = from u in query.Users
select u;
if (!string.IsNullOrEmpty(a))
{
q = q.Where(p => p.name == a);
}
if (!string.IsNullOrEmpty(b))
{
q = q.Where(p => p.age == b);
}
if (!string.IsNullOrEmpty(c))
{
q = q.Where(p => p.sex == c);
}
if (!string.IsNullOrEmpty(d))
{
q = q.Where(p => p.address == d);
}
q.ToList(); //上边的所有if,只有到此处才会执行
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/q107770540/archive/2010/07/09/5724013.aspx
WHERE IsOnLine=1
AND ((@Name IS NULL) OR ([Name] LIKE @Name))
AND ((@City IS NULL) OR ([City]=@City ))
AND .......
where IsOnline=1
and isnull(@Name,QQTable.Name)
and isnull(@City,QQTable.City)
and isnull(@Age,QQTable.Age)
更多的自己加上去