Den her kode skulle gerne illustrere teknikken (selvom det er en console app og ikke en ASP.NET app):
Imports System
Imports System.Collections.Generic
Imports MySql.Data.MySqlClient
Namespace E
Public Class MenuItem
Public Property Number As Integer
Public Property Title As String
Public ReadOnly SubMenu As IList(Of MenuItem) = New List(Of MenuItem)()
Public Sub New(number As Integer, title As String)
Me.Number = number
Me.Title = title
End Sub
End Class
Public Class Program
Public Shared Sub SetUp()
Using con As New MySqlConnection("Server=localhost;Database=Test;User Id=root;Password=")
con.Open()
Using cre As New MySqlCommand("CREATE TABLE menu (id INTEGER NOT NULL, parent_id INTEGER, title VARCHAR(32), PRIMARY KEY(id))", con)
cre.ExecuteNonQuery()
End Using
Using ins As New MySqlCommand("INSERT INTO menu VALUES(@id,@parent_id,@title)", con)
ins.Parameters.Add("@id", MySqlDbType.Int32)
ins.Parameters.Add("@parent_id", MySqlDbType.Int32)
ins.Parameters.Add("@title", MySqlDbType.VarChar, 32)
ins.Parameters("@id").Value = 1
ins.Parameters("@parent_id").Value = 0
ins.Parameters("@title").Value = "Menu 1"
ins.ExecuteNonQuery()
ins.Parameters("@id").Value = 2
ins.Parameters("@parent_id").Value = 0
ins.Parameters("@title").Value = "Menu 2"
ins.ExecuteNonQuery()
ins.Parameters("@id").Value = 3
ins.Parameters("@parent_id").Value = 1
ins.Parameters("@title").Value = "Menu 1.1"
ins.ExecuteNonQuery()
ins.Parameters("@id").Value = 4
ins.Parameters("@parent_id").Value = 1
ins.Parameters("@title").Value = "Menu 1.2"
ins.ExecuteNonQuery()
ins.Parameters("@id").Value = 5
ins.Parameters("@parent_id").Value = 3
ins.Parameters("@title").Value = "Menu 1.1.1"
ins.ExecuteNonQuery()
End Using
End Using
End Sub
Public Shared Function LoadMenu() As MenuItem
Dim aux As IDictionary(Of Integer, MenuItem) = New Dictionary(Of Integer, MenuItem)()
Dim root As New MenuItem(0, "<root>")
aux.Add(0, root)
Using con As New MySqlConnection("Server=localhost;Database=Test;User Id=root;Password=")
con.Open()
Using sel As New MySqlCommand("SELECT id,parent_id,title FROM menu", con)
Using rdr As MySqlDataReader = sel.ExecuteReader()
While rdr.Read()
Dim id As Integer = CInt(rdr("id"))
Dim parent_id As Integer = CInt(rdr("parent_id"))
Dim title As String = DirectCast(rdr("title"), String)
Dim menu As New MenuItem(id, title)
aux.Add(id, menu)
aux(parent_id).SubMenu.Add(menu)
End While
End Using
End Using
End Using
Return root
End Function
Public Shared Sub PrintMenu(indent As String, menu As MenuItem)
Console.WriteLine(indent & menu.Title)
For Each onesubmenu As MenuItem In menu.SubMenu
PrintMenu(indent & " ", onesubmenu)
Next
End Sub
Public Shared Sub TearDown()
Using con As New MySqlConnection("Server=localhost;Database=Test;User Id=root;Password=")
con.Open()
Using drp As New MySqlCommand("DROP TABLE menu", con)
drp.ExecuteNonQuery()
End Using
End Using
End Sub
Public Shared Sub Main(args As String())
SetUp()
Dim root As MenuItem = LoadMenu()
PrintMenu("", root)
TearDown()
Console.ReadKey()
End Sub
End Class
End Namespace