将菜单项保存在数据库中 [英] Saving Menu items in database

查看:144
本文介绍了将菜单项保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜单如何在数据库中另存为父菜单和子菜单,如何在vb.net中创建父菜单与子菜单之间的关系
任何人都可以帮助我plz

How menus save in data base as parent menu and as sub menus, how we create relationship between parent menus to sub menus in vb.net
any one help me plz

推荐答案



了解以下示例并尝试自己:thumbsup:

Hi,

Understand the following sample and try yourself :thumbsup:

Private Sub PopulateMenu()
    Dim ds As DataSet = GetDataSetForMenu()

    Dim menu As New Menu()

    For Each parentItem As DataRow In ds.Tables("Categories").Rows
        Dim categoryItem As New MenuItem(DirectCast(parentItem("CategoryName"), String))
        menu.Items.Add(categoryItem)

        For Each childItem As DataRow In parentItem.GetChildRows("Children")
            Dim childrenItem As New MenuItem(DirectCast(childItem("ProductName"), String))
            categoryItem.ChildItems.Add(childrenItem)
        Next
    Next

    Panel1.Controls.Add(menu)
    Panel1.DataBind()

End Sub

Private Function GetDataSetForMenu() As DataSet
    Dim myConnection As New SqlConnection(GetConnectionString())
    Dim adCat As New SqlDataAdapter("SELECT * FROM Categories", myConnection)
    Dim adProd As New SqlDataAdapter("SELECT * FROM Products", myConnection)

    Dim ds As New DataSet()
    adCat.Fill(ds, "Categories")
    adProd.Fill(ds, "Products")

    ds.Relations.Add("Children", ds.Tables("Categories").Columns("CategoryID"), ds.Tables("Products").Columns("CategoryID"))
    Return ds

End Function



[修改:如果您要提供代码,请学习使用前置标签...他们的朋友!]



[Modified: if you''re going to provide code, learn to use the pre tags...their your friend!]


我猜您有一个模板并且您想要将该模板用于多个页面,并且每个页面可以具有不同的菜单.

最简单的方法是一张桌子.它也不必是花哨的桌子.看起来可能像这样:

第1栏:第
页 第2栏:商品名称
第3栏:商品ID
第4栏:项目链接
第5列:父级ID
第6栏:订购

对于每个根菜单,只需将parent设置为null或类似"Root"的名称.

然后,在代码中构建页面时,首先查找该页面的所有Root项目,然后以指定顺序将它们添加到菜单中.添加每个项目时,请检查它是否是其他任何项目的父项,并添加它们.

您可以使用多个表并建立关系来完成此操作,但是对于每个新级别,您都必须有一个单独的表;如果您添加的页面包含更多级别,则必须添加更多表.
I''m guessing that you have a template and you want to use that template for multiple pages and each page can have different menus.

The easiest way to do that is with one table. It doesn''t have to be a fancy table either. It could look like this:

Column 1: Page
Column 2: Item Name
Column 3: Item ID
Column 4: Item Link
Column 5: Parent ID
Column 6: Order

For each root menu, you would just set parent either to null or something like "Root".

Then, when you build the page in the code, you look up all of the Root items for that page first, then add them to the menu in the specified order. As you add each item, check to see if it is a parent to any other items and add them.

You could do it with multiple tables and setting up relationships, but then you have to have a separate table for each new level and if you add a page with more levels, then you have to add more tables.


这篇关于将菜单项保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆