记录一些常用的简单方法,避免后期因为小细节未处理回溯修改bug问题:
1.处理数组遍历出的值有可能是null,
我们都知道数据库(如mysql)字段值为null时,传到程序的后台,编译器会自动处理为DBnull,当我们确定有些字段值是选填项(换言之,有时有值,有时为null),遍历的时候可以这样处理:
////// 获取直属下级用户 /// /// 上级用户代码 ////// /// author:test /// 2018-04-27 /// public MyUserInfo Get_LevelUserInfo() { try { //标明读写操作 IDbShell shell =Common.GetIDbShell(ActionType.Write); string sql = ""; DataTable dt = shell.ExecuteQueryDataTable(sql); if (dt != null && dt.Rows.Count > 0) { user = new UserInfo(); user.TypeId = Convert.ToInt32(dt.Rows[0]["type_id"]); user.ParentCode = Convert.ToString(dt.Rows[0]["parent_uid"]); //处理null时方法 user.Contact_Name = dt.Rows[0]["contact_name"] == null? null:Convert.ToString(dt.Rows[0]["contact_name"]); } return user; } catch (Exception ex) { Common.SaveErrorLog(ex.Message); throw ex; } }
2.处理多列(sum、count)统计数据
-- 处理统计时两列数据求count值SELECT (count(gender)+COUNT(age)) as tongji FROM `ls` where gender=1;-- 处理统计时两列数据求sum值SELECT (sum(gender)+sum(age)) as tongji FROM `ls` where gender=1;
3.处理求和中有null的统计数据
-- 处理统计时含null数据求sum值select ifnull(sum(o.lieone),0) as t_lieone, ifnull(sum(o.lietwo),0) as t_lietwo, ifnull(sum(o.liethree),0) as t_liethree, ifnull(sum(o.liefour),0) as t_liefour, ifnull(sum(o.five),0) as t_fivefrom userinfo o