技术贴!求高手!C#查询sql数据 在C#中查询数据库。但是条件是可变的。where 后面跟的一个或多个条件,因为条件个数不确定,所以语句不知道怎么写 求高手教我!!!QQ348771523 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sql = "select * from table where ";sql += "x = 1 and ";sql += "y = 'hello' and ";sql += "z < 20 and ";...sql += "1 = 1"; string strSQL = "Select * From 表"if(条件A) strSQL += 语句Aelse if(条件B) strSQL += 语句B。 1楼的方法再稍微改进一下更好:sql = "select * from table where 1=1 ";sql += " and x = 1 ";sql += " and y = 'hello' ";sql += " and z < 20 ";...这样,即使where条件一个都没有都不会出错。 就是动态查询,以前是sql拼接字符串,象楼上的一样,但用在WEB上要注意SQL注入用linq的话可以用表达式树 sql="select * from table where datetimes1=@datetime1 and datetime2=@datetime2"query.Parameters.Add("@datetime1", SqlDbType.Date);query.Parameters["@datetime1"].Value = dateTimepicker.Value;query.Parameters.Add("@datetime2", SqlDbType.Date);query.Parameters["@datetime2"].Value = dateTimepicker1.Value; 请问信号量Release方法 (Int32)什么意思 控制台应用程序下判断用户是否通过键盘输入内容 求教这个软件的部分所用到的控件和技术 c#如何接收oracle包中返回的cursor类型数据? 在页面上 加了一个Repeater 页面就报错了 索引指示器的问题 关于如何从外部停止多线程的执行 C#字符串的一个简单问题 请教一个C#实现多重继承的建议! 两个关于水晶报表的问题 WCF使用netTcpBinding时 出现10060错误,但更新服务应用没有问题.. 水晶报表直连数据库的问题
sql += "y = 'hello' and ";
sql += "z < 20 and ";
...sql += "1 = 1";
if(条件A)
strSQL += 语句A
else if(条件B)
strSQL += 语句B
。
sql += " and y = 'hello' ";
sql += " and z < 20 ";
...这样,即使where条件一个都没有都不会出错。
用linq的话可以用表达式树
query.Parameters.Add("@datetime1", SqlDbType.Date);
query.Parameters["@datetime1"].Value = dateTimepicker.Value;
query.Parameters.Add("@datetime2", SqlDbType.Date);
query.Parameters["@datetime2"].Value = dateTimepicker1.Value;