首页 > 其他 > 详细

通用权限管理系统底层的参数化查询方法如何使用

时间:2015-04-30 21:40:13      阅读:194      评论:0      收藏:0      [点我收藏+]

通用权限管理系统底层的数据访问方法支持参数化查询。

先前没有使用参数化查询的语句是这样的

            string conmmondText = " SELECT A.SITE_ID AID, A.SITE_NAME ANAME, B.SITE_ID BID, B.SITE_NAME BNAME"
                         + " FROM (SELECT 1 AS ID, SITE_NAME, SITE_ID"
                         + " FROM AREA_SUB"
                         + " WHERE AREA_ID = {0}"
                         + " AND DELETIONSTATECODE = 0) A"
                         + " LEFT JOIN (SELECT 1 AS ID, SITE_NAME, SITE_ID"
                         + " FROM AREA_SUB"
                         + " WHERE AREA_ID = {1}"
                         + " AND DELETIONSTATECODE = 0) B ON A.ID = B.ID";

            conmmondText= string.Format(conmmondText, array[0], array[1]);
            var dt = dbHelper.Fill(conmmondText);

 现数据库管理员要求SQL语句要使用参数化查询,改造后的语句

            string conmmondText= " SELECT A.SITE_ID AID, A.SITE_NAME ANAME, B.SITE_ID BID, B.SITE_NAME BNAME"
                         + " FROM (SELECT 1 AS ID, SITE_NAME, SITE_ID"
                         + " FROM AREA_SUB"
                         + " WHERE AREA_ID = {0}"
                         + " AND DELETIONSTATECODE = 0) A"
                         + " LEFT JOIN (SELECT 1 AS ID, SITE_NAME, SITE_ID"
                         + " FROM AREA_SUB"
                         + " WHERE AREA_ID = {1}"
                         + " AND DELETIONSTATECODE = 0) B ON A.ID = B.ID";
            IDbDataParameter[] dbParameters = new IDbDataParameter[] { 
                                                        ztoQuotePriceManager.DbHelper.MakeParameter("AREA_ID_0", array[0]),
                                                        ztoQuotePriceManager.DbHelper.MakeParameter("AREA_ID_1", array[1])
                                                    };
            var  dt = dbHelper.Fill(conmmondText, dbParameters);

这样改造后可以防止SQL注入。

另外一个改造的语句参考

            //List<AREA_SUBEntity> sendAreaSubList = areaSubManager.GetList<AREA_SUBEntity>(AREA_SUBEntity.FieldAREA_ID + "=" + ztoQuotePriceEntity.SEND_AREA_ID + "");
            //List<AREA_SUBEntity> dispAreaSubList = areaSubManager.GetList<AREA_SUBEntity>(AREA_SUBEntity.FieldAREA_ID + "=" + ztoQuotePriceEntity.DISP_AREA_ID + "");
            List<AREA_SUBEntity> sendAreaSubList = areaSubManager.GetList<AREA_SUBEntity>(new KeyValuePair<string, object>(AREA_SUBEntity.FieldAREA_ID, ztoQuotePriceEntity.SEND_AREA_ID));
            List<AREA_SUBEntity> dispAreaSubList = areaSubManager.GetList<AREA_SUBEntity>(new KeyValuePair<string, object>(AREA_SUBEntity.FieldAREA_ID, ztoQuotePriceEntity.DISP_AREA_ID));

 

建议大家在使用底层时,尽量使用带参数化查询的方法。

通用权限管理系统底层的参数化查询方法如何使用

原文:http://www.cnblogs.com/hnsongbiao/p/4469893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!