当前位置:编程学习 > C#/ASP.NET >>

DAL层执行SQL语句时,把int类型默认值为0的字段加入了查询,结果出错,求解!!

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的字段
求解决办法!
--------------------编程问答-------------------- if(id!="0"&&id!="")
{
   mfcsk.BanciId=id
} --------------------编程问答--------------------  通用查询方法的遍历里面 有拼接字符串的操作,单步调试下就知道原因了 --------------------编程问答--------------------
引用 2 楼 lizeyuan8238386 的回复:
 通用查询方法的遍历里面 有拼接字符串的操作,单步调试下就知道原因了

调试过……
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是错的。。 --------------------编程问答-------------------- 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 楼 ly_sl 的回复:
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就可以了


不好意思。。那个where是写帖子的时候漏写的。 --------------------编程问答-------------------- 优化一下
para +=" and "+ prop.Name +"=@"+ prop.Name


....
"Select * From "+TableName +" WHere 1=1 "+  para 

如果还有错,那就发最后转<T>的代码出来 --------------------编程问答-------------------- 楼主赶紧删除这种代码,老老实实写原生的sql 或者学EF。 --------------------编程问答-------------------- 楼主的意思是当count和price等于0的时候,sql的条件就应该没有这两个字段,对吗?
如果你非要这么写的话,就自己加上等于0的判断,等于0的时候让这两个字段对应的变量为null,这样出来的sql就符合你的要求了 --------------------编程问答-------------------- 除
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,