web层:BLL层:DAL层:SQL通用查询方法:
原本调用的SQL语句应该是select * from table where id = xxxx。结果出来的语句是select * from table id = xxxx and count = 0 and price = 0
方法只排除了model中默认值为null的,没有排除默认值为0的字段
求解决办法!
原本调用的SQL语句应该是select * from table where id = xxxx。结果出来的语句是select * from table id = xxxx and count = 0 and price = 0
方法只排除了model中默认值为null的,没有排除默认值为0的字段
求解决办法!
{
mfcsk.BanciId=id
}
foreach (PropertyDescriptor prop in properties)
{
if (prop.GetValue(t) != null )
{
para += prop.Name + "=" + "@" + prop.Name + " and ";
list.Add(new SqlParameter("@" + prop.Name, prop.GetValue(t)));
//break;
}
}
para = para.Substring(0, para.Length - 4);
string sqls = "select * from " + TableName + " where " + para;
这一段的prop.GetValue(t) != null只将传过来的T中,值为null的排除,没有排除值为0的。
解决办法中,prop.GetValue(t) != 0是错的。。
这句应该是少了个where,然后这里面是要查询count ,price 这两个是0的。假如LZ是想再排除model不等于0的,那直接改成select * from table where id = xxxx and count = 0 and price = 0 and model<>0 and model is not null就可以了
不好意思。。那个where是写帖子的时候漏写的。
para +=" and "+ prop.Name +"=@"+ prop.Name
....
"Select * From "+TableName +" WHere 1=1 "+ para 如果还有错,那就发最后转<T>的代码出来
如果你非要这么写的话,就自己加上等于0的判断,等于0的时候让这两个字段对应的变量为null,这样出来的sql就符合你的要求了