码迷,mamicode.com
首页 > 其他好文 > 详细

菜单数据(树形)结构的使用-- ---数据的列存储转换为行存储

时间:2014-05-01 09:35:12      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   javascript   ext   width   string   get   

一、菜单数据表中的存储结构

     mamicode.com,码迷

二、转换后的数据结构

    mamicode.com,码迷

三、 转换过程

     1、确定菜单数据的最大级别

mamicode.com,码迷
        /// <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);
                    }
                }
            }
        }
mamicode.com,码迷

     2、 构造存储该数据的表结构

mamicode.com,码迷
        /// <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;
        }
mamicode.com,码迷

三、 获得菜单数据的全部信息

      

mamicode.com,码迷
   /// <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;
        }
mamicode.com,码迷

四、将数据的树形存储转为线性存储

    

mamicode.com,码迷
        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);
                }
            }
   
        }
mamicode.com,码迷

五、总结(整体代码概览)

mamicode.com,码迷
    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;
        }

    }
mamicode.com,码迷

六、运行效果;如下图

mamicode.com,码迷

 数据结构:

mamicode.com,码迷
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]
mamicode.com,码迷

 

附源代码:下载

 

 

 

 

 

 

 

 

 

 

 

 

 

    

 

 

 

菜单数据(树形)结构的使用-- ---数据的列存储转换为行存储,码迷,mamicode.com

菜单数据(树形)结构的使用-- ---数据的列存储转换为行存储

标签:style   blog   class   code   java   tar   javascript   ext   width   string   get   

原文地址:http://www.cnblogs.com/tianjinquan/p/3702390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!