public static List <VOMenu> ListByParentId(string xConnectionString, int xParentId, int xRoleId) { //return value List <VOMenu> list = new List <VOMenu>(); try { using (SqlConnection objConn = new SqlConnection(xConnectionString)) { //Open DataBase objConn.Open(); //ถ่ายทอด Command Class มาจาก Connection ที่เปิดไว้ SqlCommand objComm = objConn.CreateCommand(); if (xParentId == 0) { objComm.CommandText = @" select MenuId, Code, ParentId, LinkURL, PageCode from menu where ParentId is null"; } else { objComm.CommandText = @" select MenuId, Code, ParentId, LinkURL, PageCode from menu where ParentId = @ParentId and MenuId not in (select menuid from view_rolemenu where roleId = @roleId and parentid = @ParentId)"; objComm.Parameters.Add("@roleId", SqlDbType.Int).Value = xRoleId; objComm.Parameters.Add("@ParentId", SqlDbType.Int).Value = xParentId; } SqlDataReader objReader = objComm.ExecuteReader(); while (objReader.Read()) { VOMenu objRoleMenu = new VOMenu(); objRoleMenu.MenuId = (int)objReader["MenuId"]; objRoleMenu.Code = (string)objReader["Code"]; objRoleMenu.ParentId = objReader["ParentId"] == DBNull.Value ? 0 : (int)objReader["ParentId"]; objRoleMenu.LinkURL = (string)objReader["LinkURL"]; objRoleMenu.PageCode = objReader["PageCode"] == DBNull.Value ? "" : (string)objReader["PageCode"]; list.Add(objRoleMenu); } } } catch (Exception) { } return(list); }
public int GetParentId(string xConnectionString) { int valueReturn = 0; try { using (SqlConnection objConn = new SqlConnection(xConnectionString)) { //open DataBase objConn.Open(); //ถ่ายทอด Command Class มาจาก Connection ที่เปิดไว้ SqlCommand objComm = objConn.CreateCommand(); objComm.CommandText = @" select * from menu where menuId = @menuId"; objComm.Parameters.Add("@menuId", SqlDbType.Int).Value = this.MenuId; SqlDataReader objReader = objComm.ExecuteReader(); VOMenu objMenu = new VOMenu(); while (objReader.Read()) { objMenu.MenuId = (int)objReader["MenuId"]; objMenu.Code = (string)objReader["Code"]; objMenu.LinkURL = (string)objReader["LinkURL"]; objMenu.ParentId = objReader["ParentId"] == DBNull.Value ? 0 : (int)objReader["ParentId"]; objMenu.IsTop = (bool)objReader["IsTop"]; objMenu.PageCode = objReader["PageCode"] == DBNull.Value ? "" : (string)objReader["PageCode"]; } valueReturn = objMenu.ParentId == 0 ? objMenu.MenuId : objMenu.ParentId; } } catch (Exception) { valueReturn = 0; } return(valueReturn); }