/// <summary> /// 添加角色 /// </summary> public int AddRole(Model.Role role) { ZGZY.Model.Role roleCompare = dal.GetRoleByRoleName(role.RoleName); if (roleCompare != null) { throw new Exception("已经存在此角色!"); } return(dal.AddRole(role)); }
/// <summary> /// 根据角色名获取角色 /// </summary> public Model.Role GetRoleByRoleName(string roleName) { string sql = "select top 1 * from tbRole where RoleName = @RoleName"; ZGZY.Model.Role role = null; DataTable dt = ZGZY.Common.SqlHelper.GetDataTable(ZGZY.Common.SqlHelper.connStr, CommandType.Text, sql, new SqlParameter("@RoleName", roleName)); if (dt.Rows.Count > 0) { role = new ZGZY.Model.Role(); DataRowToModel(role, dt.Rows[0]); return(role); } else { return(null); } }
/// <summary> /// 把DataRow行转成实体类对象 /// </summary> private void DataRowToModel(ZGZY.Model.Role model, DataRow dr) { if (!DBNull.Value.Equals(dr["Id"])) { model.Id = int.Parse(dr["Id"].ToString()); } if (!DBNull.Value.Equals(dr["RoleName"])) { model.RoleName = dr["RoleName"].ToString(); } if (!DBNull.Value.Equals(dr["Description"])) { model.Description = dr["Description"].ToString(); } if (!DBNull.Value.Equals(dr["AddDate"])) { model.AddDate = Convert.ToDateTime(dr["AddDate"]); } if (!DBNull.Value.Equals(dr["ModifyDate"])) { model.ModifyDate = Convert.ToDateTime(dr["ModifyDate"]); } }