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的字段
求解决办法!

解决方案 »

  1.   

    if(id!="0"&&id!="")
    {
       mfcsk.BanciId=id
    }
      

  2.   

    调试过……
    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是错的。。
      

  3.   

    select * from table id = xxxx and count = 0 and price = 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就可以了
      

  4.   


    不好意思。。那个where是写帖子的时候漏写的。
      

  5.   

    优化一下
    para +=" and "+ prop.Name +"=@"+ prop.Name
    ....
    "Select * From "+TableName +" WHere 1=1 "+  para 如果还有错,那就发最后转<T>的代码出来
      

  6.   

    楼主赶紧删除这种代码,老老实实写原生的sql 或者学EF。
      

  7.   

    楼主的意思是当count和price等于0的时候,sql的条件就应该没有这两个字段,对吗?
    如果你非要这么写的话,就自己加上等于0的判断,等于0的时候让这两个字段对应的变量为null,这样出来的sql就符合你的要求了