动态添加菜单mainmenu
VB.NET。我在数据库中那了一张菜单管理表,跟树形结构表差不多,如何从数据库中读取表资料对mainmenu菜单进行动态添加呢?我想用递归涵数应该可以实现这个功能吧?哪位高手能帮忙解决一下!
数据库结构
menu_id menu_text menu_parid
010 1 Null
010-001 11 010
010-002 12 010
010-002-001 121 010-002
010-002-002 122 010-002
020 2 Null
020-001 21 020
020-002 22 020
[解决办法]
不好意思,恕我直言,呵呵。
Public Class Form1 Private ConnString As String = "Data Source=(local);Initial Catalog=tempdb;Integrated Security=True" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim theMenu As New MainMenu Dim sql As String = "select * from testtable where menu_parid is null " Dim rst As DataSet = ExecuteSQL(sql) If rst IsNot Nothing Then Dim dt As DataTable = rst.Tables(0) For i As Integer = 0 To dt.Rows.Count - 1 Dim mMenuItem As New MenuItem(dt.Rows(i).Item("menu_text").ToString) Call GetMenuItem(mMenuItem, dt.Rows(i).Item("menu_id").ToString) theMenu.MenuItems.Add(mMenuItem) Next End If Me.Menu = theMenu End Sub Private Function GetMenuItem(ByVal item As MenuItem, ByVal id As String) As String Dim sql As String = "select * from testtable where menu_parid='" + id + "'" Dim rst As DataSet = ExecuteSQL(sql) If rst IsNot Nothing Then Dim dt As DataTable = rst.Tables(0) For i As Integer = 0 To dt.Rows.Count - 1 Dim mMenuItem As New MenuItem(dt.Rows(i).Item("menu_text").ToString) Call GetMenuItem(mMenuItem, dt.Rows(i).Item("menu_id")) item.MenuItems.Add(mMenuItem) Next End If End Function Public Function ExecuteSQL(ByVal strSQL As String) As DataSet Dim sqlConnection As New System.Data.SqlClient.SqlConnection(Me.ConnString) Dim rst As System.Data.DataSet = Nothing Try sqlConnection.Open() rst = New System.Data.DataSet Dim adpt As New System.Data.SqlClient.SqlDataAdapter(strSQL, sqlConnection) adpt.Fill(rst) : adpt.Dispose() Return rst Catch ex As Exception Throw New Exception(ex.Message) Finally sqlConnection.Close() End Try Return rst End FunctionEnd Class