一、在Foreach之前要判断是否为空。
常常从数据库里取出来表就直接用了,很少记得判断是否有值。不严谨的说。
专业人员写的是酱滴。。。
DataLayer.PMSecDataSet.PMSECRequestDataTable requestDataTable = adapter.up_PMSec_PMSECRequestSelectByMasterIDs(masterIds); if (requestDataTable != null && requestDataTable.Count > )
{
...
}
二、当数据库中允许为空。
话说,上次已经说过的啦。。。但是这次更加明确,要用Nullable<int>.
Nullable<int> intNull = null;
DataLayer.PMSecDataSet.PMSECRequestDataTable requestDataTable = adapter.up_PMSec_PMSECRequestSelectByMasterIDs(masterIds); if (requestDataTable != null && requestDataTable.Count > )
{ foreach (DataLayer.PMSecDataSet.PMSECRequestRow dr in requestDataTable.Rows)
{
PMSecRequestInfo requestInfo = new PMSecRequestInfo();
requestInfo.RequestMasterID = dr.RequestMasterID;
requestInfo.EmployeeID = dr.EmployeeID;
Employee requestEmployee = new Employee(dr.EmployeeID);
requestInfo.DisplayName = requestEmployee.DisplayName;
requestInfo.StatusType = dr.StatusType;
requestInfo.CompanyID = dr.CompanyID;
requestInfo.WorkStoreID = dr.WorkStoreID;
requestInfo.JobCode = dr.JobCode;
requestInfo.RequestedSecurityLevel = dr.IsRequestedSecurityLevelNull() ? intNull : dr.RequestedSecurityLevel;
requestInfo.IsSecurityAdmin = dr.IsSecurityAdmin;
requestInfo.CreatedBy = dr.CreatedBy;
requestInfo.CreatedOn = dr.CreatedOn; requestInfoList.Add(requestInfo);
}
}
string类无需此处理。
三、操作数据库时
当含有update等更改数据库内容的语句存在时,如果下面操作仍与数据库有关,考虑到程序的扩展性,可能出现上述语句删除了某些数据,下面语句出现异常,或者产生错误结果,等等。
譬如下面两段代码,前者是我的做法,后者为Cynthia的做法,一见高下啊啊啊。我写的好像是舒克和贝塔里那只笨猫,喵,~~~~(>_<)~~~~ 。
public void ApporveOrDeclineRequest(string masterIds, string statusType, int loginEid)
{
PMSECRequestPendingTaskTableAdapter adapter = new PMSECRequestPendingTaskTableAdapter();
adapter.up_PMSec_PMSECRequestUpdate(masterIds, statusType, loginEid); // Send Email
PandaRG.Common.OrgChart.Employee logInEmployee = new Common.OrgChart.Employee(loginEid); if (statusType == RequestStatusType.APPROVED.ToString())
{
EmailSendHelper.SentEmailbyEmailType(RequestStatusType.APPROVED, GetRequestInfoListByRequestMasterIds(masterIds), logInEmployee); }
else if(statusType==RequestStatusType.DECLINED.ToString())
{
EmailSendHelper.SentEmailbyEmailType(RequestStatusType.DECLINED, GetRequestInfoListByRequestMasterIds(masterIds), logInEmployee);
}
}
public void UpdatePMSECRequest(string masterIds, RequestStatusType statusType, Employee loginEmployee)
{
PMSECRequestPendingTaskTableAdapter adapter = new PMSECRequestPendingTaskTableAdapter(); List<PMSecRequestInfo> requestInfoList = GetRequestInfoListByRequestMasterIds(masterIds);
adapter.up_PMSec_PMSECRequestUpdate(masterIds, statusType.ToString(), loginEmployee.EmployeeId);
EmailSendHelper.SentEmailbyEmailType(statusType, requestInfoList, loginEmployee);
}
四、 λ 表达式
好复杂的说,不老明白的,计入λ 番外篇吧。
( ^_^ )/~~