首页 > 代码库 > 菜单数据(树形)结构的使用-- ---数据的列存储转换为行存储
菜单数据(树形)结构的使用-- ---数据的列存储转换为行存储
一、菜单数据表中的存储结构
二、转换后的数据结构
三、 转换过程
1、确定菜单数据的最大级别
/// <summary> /// 获得Nature定义的最大目录级别,以便于确定Nature的DataTable表结构中的列数目 /// </summary> /// <returns></returns> public int GetMaxNatureLevel() { if (dtNature == null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } int maxLevel = 0; foreach (DataRow dr in dtNature.Rows) { int currentLevel = 0; GetLevelByNatureID(dr["natureid"].ToString(), ref currentLevel); if (currentLevel > maxLevel) { maxLevel = currentLevel; } } return maxLevel; } public void GetLevelByNatureID(string natureid, ref int level) { if (dtNature == null || dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } if (dtNature.Rows.Count > 0) { DataRow[] drs = dtNature.Select(string.Format("natureid=‘{0}‘", natureid)); if (drs.Count() > 0) { level++; string parentid = drs[0]["parentid"].ToString(); DataRow[] drst = dtNature.Select(string.Format("natureid=‘{0}‘", parentid)); if (drst.Length > 0) { GetLevelByNatureID(parentid, ref level); } } } }
2、 构造存储该数据的表结构
/// <summary> /// 依据Nature目录的最大级别,声明表的存储结构 /// </summary> /// <returns></returns> public DataTable DeclareNatureTable() { DataTable dt = new DataTable(); if (dtNature == null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } dt.Columns.Add("natureid"); int MaxLevel = GetMaxNatureLevel(); for (int i = 1; i <= MaxLevel; i++) { dt.Columns.Add("nature" + i.ToString()); } return dt; }
三、 获得菜单数据的全部信息
/// <summary> /// 获得数据库中Nature的所有信息 /// </summary> /// <returns></returns> public DataTable GetNatureData() { DataTable dt = new DataTable(); try { string sQuery = string.Format(" SELECT natureid,naturenm,parentid,seqnumbr FROM dbo.CRNATUREM0 WHERE dsblflag =0 ORDER BY seqnumbr "); Database db = DatabaseFactory.CreateDatabase(); DataSet ds = db.ExecuteDataSet(CommandType.Text, sQuery); if (ds != null) { dt = ds.Tables[0]; } } catch (Exception ex) { MessageBox.Show("查询数据失败:" + ex.Message); } return dt; }
四、将数据的树形存储转为线性存储
public DataTable GetNatureTable() { try { if (dtNature != null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } dtResult = DeclareNatureTable(); // 声明表的结构 foreach (DataRow dr in dtNature.Rows) { DataRow drn = dtResult.NewRow(); string natureid = dr["natureid"].ToString(); drn["natureid"] = natureid; int level = 0; GetLevelByNatureID(natureid, ref level); int leve11 = 0; Dictionary<int, string> natureDict = new Dictionary<int, string>(); GetNatureValsByID(natureid, ref leve11,natureDict); foreach (KeyValuePair<int, string> keyNature in natureDict) { string colName = "nature" + (level - keyNature.Key).ToString(); drn[colName] = keyNature.Value; } dtResult.Rows.Add(drn); } } catch (Exception ex) { MessageBox.Show("查询数据失败:" + ex.Message); } return dtResult; } /// <summary> /// 依据Natureid 获得相应的所有级类目信息 /// </summary> /// <param name="natureid"></param> /// <param name="level"></param> /// <param name="natureDict"></param> public void GetNatureValsByID(string natureid, ref int level,Dictionary<int, string> natureDict) { if (dtNature != null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } if (dtNature != null && dtNature.Rows.Count > 0) { DataRow[] drs = dtNature.Select(string.Format("natureid=‘{0}‘", natureid)); natureDict.Add(level, drs[0]["naturenm"].ToString()); natureid = drs[0]["parentid"].ToString(); if(natureid.Trim().Length > 0) { ++level; GetNatureValsByID(natureid, ref level, natureDict); } } }
五、总结(整体代码概览)
public class NatureData { private DataTable dtNature = new DataTable(); private DataTable dtResult = new DataTable(); public NatureData() { } public DataTable GetNatureTable() { try { if (dtNature != null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } dtResult = DeclareNatureTable(); // 声明表的结构 foreach (DataRow dr in dtNature.Rows) { DataRow drn = dtResult.NewRow(); string natureid = dr["natureid"].ToString(); drn["natureid"] = natureid; int level = 0; GetLevelByNatureID(natureid, ref level); int leve11 = 0; Dictionary<int, string> natureDict = new Dictionary<int, string>(); GetNatureValsByID(natureid, ref leve11,natureDict); foreach (KeyValuePair<int, string> keyNature in natureDict) { string colName = "nature" + (level - keyNature.Key).ToString(); drn[colName] = keyNature.Value; } dtResult.Rows.Add(drn); } } catch (Exception ex) { MessageBox.Show("查询数据失败:" + ex.Message); } return dtResult; } /// <summary> /// 依据Natureid 获得相应的所有级类目信息 /// </summary> /// <param name="natureid"></param> /// <param name="level"></param> /// <param name="natureDict"></param> public void GetNatureValsByID(string natureid, ref int level,Dictionary<int, string> natureDict) { if (dtNature != null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } if (dtNature != null && dtNature.Rows.Count > 0) { DataRow[] drs = dtNature.Select(string.Format("natureid=‘{0}‘", natureid)); natureDict.Add(level, drs[0]["naturenm"].ToString()); natureid = drs[0]["parentid"].ToString(); if(natureid.Trim().Length > 0) { ++level; GetNatureValsByID(natureid, ref level, natureDict); } } } public void GetLevelByNatureID(string natureid, ref int level) { if (dtNature == null || dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } if (dtNature.Rows.Count > 0) { DataRow[] drs = dtNature.Select(string.Format("natureid=‘{0}‘", natureid)); if (drs.Count() > 0) { level++; string parentid = drs[0]["parentid"].ToString(); DataRow[] drst = dtNature.Select(string.Format("natureid=‘{0}‘", parentid)); if (drst.Length > 0) { GetLevelByNatureID(parentid, ref level); } } } } /// <summary> /// 获得Nature定义的最大目录级别,以便于确定Nature的DataTable表结构中的列数目 /// </summary> /// <returns></returns> public int GetMaxNatureLevel() { if (dtNature == null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } int maxLevel = 0; foreach (DataRow dr in dtNature.Rows) { int currentLevel = 0; GetLevelByNatureID(dr["natureid"].ToString(), ref currentLevel); if (currentLevel > maxLevel) { maxLevel = currentLevel; } } return maxLevel; } /// <summary> /// 依据Nature目录的最大级别,声明表的存储结构 /// </summary> /// <returns></returns> public DataTable DeclareNatureTable() { DataTable dt = new DataTable(); if (dtNature == null && dtNature.Rows.Count == 0) { dtNature = GetNatureData(); } dt.Columns.Add("natureid"); int MaxLevel = GetMaxNatureLevel(); for (int i = 1; i <= MaxLevel; i++) { dt.Columns.Add("nature" + i.ToString()); } return dt; } /// <summary> /// 获得数据库中Nature的所有信息 /// </summary> /// <returns></returns> public DataTable GetNatureData() { DataTable dt = new DataTable(); try { string sQuery = string.Format(" SELECT natureid,naturenm,parentid,seqnumbr FROM dbo.CRNATUREM0 WHERE dsblflag =0 ORDER BY seqnumbr "); Database db = DatabaseFactory.CreateDatabase(); DataSet ds = db.ExecuteDataSet(CommandType.Text, sQuery); if (ds != null) { dt = ds.Tables[0]; } } catch (Exception ex) { MessageBox.Show("查询数据失败:" + ex.Message); } return dt; } }
六、运行效果;如下图
数据结构:
CREATE TABLE [dbo].[CRNATUREM0]( [natureid] [nvarchar](10) NOT NULL, [naturenm] [nvarchar](100) NULL, [parentid] [nvarchar](10) NULL, [seqnumbr] [int] NULL, [dsblflag] [bit] NULL, [lstupdby] [nvarchar](30) NULL, [lstupddt] [datetime] NULL, [natext] [text] NULL, CONSTRAINT [PK_CRNATUREM0] PRIMARY KEY CLUSTERED ( [natureid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
附源代码:下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。