用这种方式老是创建不成功,请各位吱吱招啊,感激::::
数据库里表的形式  菜单名称        显示名称         事件                   快捷键       父级菜单名称
MenuItemBase  基础资料(&B) NULL   NULL 0
MenuItemBase_01 新建(&N)       MenuItemBase_01 _Click NULL MenuItemBase
MenuItemBase_02 关闭(&C)       MenuItemBase_02_Click NULL MenuItemBase
MenuItemHelp 帮助(&H)        NULL NULL 0
MenuItemHelp_01 About(&A) NULL NULL MenuItemHelp            DataSet ds = new DataSet();
            string strConn = "Data Source=(local);Initial Catalog=treeTest;Integrated Security=True";            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(strConn);
                try
                {
                    sysMenu.Items.Clear(); SetMenu(sysMenu, "0");
                }
                catch
                {
                    MessageBox.Show("创建菜单时出错", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="mMain">主菜单项</param>
            /// <param name="mName">主菜单项名称</param>
            private void SetMenu(MenuStrip mMain, string mName)
            {
                
              
                    int i;
                    string SQL = "select * from table_1";
                    SqlConnection conn = new SqlConnection(strConn);
                     SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
                     sda.Fill(ds,"table_1");
                     if (ds.Tables["table_1"].Rows.Count > 0)
                    {
                        for (i = 0; i < ds.Tables["table_1"].Rows.Count; i++)
                        {
                            if (ds.Tables["table_1"].Rows[i]["FParentNo"].ToString() == "0")
                            {
                                ToolStripMenuItem mItem;
                                mItem = new ToolStripMenuItem(ds.Tables["table_1"].Rows[i]["FMenuText"].ToString());
                                mItem.Name = ds.Tables["table_1"].Rows[i]["FMenuName"].ToString();
                                mItem.Text = ds.Tables["table_1"].Rows[i]["FMenuText"].ToString();
                                mMain.Items.Add(mItem);
                                SetSubMenu((ToolStripMenuItem)mMain.Items[ds.Tables["table_1"].Rows[i]["FMenuName"].ToString()], ds.Tables["table_1"].Rows[i]["FMenuName"].ToString());
                            }
                        }
                    } 
                
             
            }
            /// <summary>
            /// 创建子菜单
            /// </summary>
            /// <param name="mItem">要创建的子菜单的父项</param>
            /// <param name="mName">要创建的子菜单的父项名称</param>
            /// 
            private void SetSubMenu(ToolStripMenuItem mItem, string mName)
            {
                try
                {
                 
                    int i;                //查询数据库
                    string SQL = "SELECT * FROM table_1 WHERE  FParentNo='" + mName + "'";
                    SqlConnection conn = new SqlConnection(strConn);
                    SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
                    sda.Fill(ds, "table_1");
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["FParentNo"].ToString() == mName)
                        {
                            ToolStripMenuItem mSubItem;
                            EventHandler eh = GetEvent(ds.Tables[0].Rows[i]["FMenuEvent"]); //获得操作事件
                            Keys key = GetShurtcutKeys(ds.Tables[0].Rows[i]["FMenuShortcutKeys"]);//获得快捷键                            //创建菜单项
                            mSubItem = new ToolStripMenuItem(ds.Tables[0].Rows[i]["FMenuText"].ToString(), null, eh, key);
                            mSubItem.Name = ds.Tables[0].Rows[i]["FMenuName"].ToString();
                            //mSubItem.Text = dt.Rows[i]["FMenuText"].ToString();
                            mItem.DropDownItems.Add(mSubItem);                        //设置子菜单
                            SetSubMenu((ToolStripMenuItem)mItem.DropDownItems[ds.Tables[0].Rows[i]["FMenuName"].ToString()], ds.Tables[0].Rows[i]["FMenuName"].ToString());
                        }
                    } 
                }
                catch
                {
                    MessageBox.Show("创建子菜单时出错", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }        /// <summary>

解决方案 »

  1.   

    我修改了一下你的表结构,添加了主键FMenuNousing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }               string strConn = "Data Source=(local);Initial Catalog=treeTest;Integrated Security=True";
            
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    sysMenu.Items.Clear(); 
                    SetMenu(sysMenu, 0);
                }
                catch
                {
                    MessageBox.Show("创建菜单时出错", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }               /// </summary>
            /// <param name="mMain">当前需要添加子菜单的菜单项或主菜单</param>
            /// <param name="mNo">父菜单项编号</param>
            private void SetMenu(object mMain, int mNo)
            {
                string cmdText = string.Format("select FMenuName, FMenuText, FMenuShortcutKeys, FMenuEvent, FParentNo, FMenuNo from table_1 where FParentNo = {0}", mNo);            SqlConnection conn = new SqlConnection(strConn);            
                SqlCommand cmd = null;            cmd = new SqlCommand(cmdText, conn);
                conn.Open();            SqlDataReader reader = cmd.ExecuteReader();            ToolStripMenuItem tsmi;            try
                {
                    while (reader.Read())
                    {
                        tsmi = new ToolStripMenuItem();
                        tsmi.Name = Convert.ToString(reader["FMenuName"]);
                        tsmi.Text = Convert.ToString(reader["FMenuText"]);                    //添加菜单点击事件,这个“tsmi_Click”必须是真实的方法名,不能放在变量中
                        //string s = "tsmi_Click" ; tsmi.Click += new EventHandler(s); //错误
                        if (reader["FMenuEvent"] != null)
                            tsmi.Click += new EventHandler(tsmi_Click);                    //保存和新菜单对应的数据库记录的主键编号,可用于为新菜单添加子菜单或为事件处理函数做准备,不一定会使用
                        tsmi.Tag = Convert.ToInt32(reader["FMenuNo"]);                    //为主菜单或菜单项添加子菜单
                        if (mMain is ToolStripMenuItem)
                            ((ToolStripMenuItem)mMain).DropDownItems.Add(tsmi);
                        else if (mMain is MenuStrip)
                            ((MenuStrip)mMain).Items.Add(tsmi);                                        SetMenu(tsmi, Convert.ToInt32(reader["FMenuNo"]));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message); ;
                }
            }        void tsmi_Click(object sender, EventArgs e)
            {
                ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;            int fMenuNo = (int)tsmi.Tag;//如果需要更复杂的操作,可以根据这个编号去数据库中查询详细信息            MessageBox.Show(string.Format("我是菜单{0}, 你点了我", tsmi.Text));
            }
        }
    }