作者qwerty147852 (小城主)
看板Visual_Basic
标题[.NET] 从SQL栏位判断产生Menu
时间Mon May 28 19:50:08 2018
请教各位大大
目前在画面上做了写死的动态menu
web:
https://imgur.com/D2ceHQW
code:
https://imgur.com/mikTb4r
SQL table:
https://imgur.com/lX9J3W1
SQL:"select 表单编号 from menu_1 where 单位编号='1' and 执行状态='1'"
筛选出有效资料
希望能改用从SQL来的资料做menu能依照SQL table里栏位(单位编号=001 & 执行状态=1 显示)
EX:menu 导览列有五大项目
●菜单管理
-T1001
-T1002
●新人训练
-T2001
-T2003
目前想到作法是这样,没写过VB不知道可不可行,又或者有其他建议?
1.对SQL做逐步select 有效状态的table(单位编号=001 & 执行状态=1 显示)
SQL:"select 表单编号 from menu_1 where 单位编号='1' and 执行状态='1'"
2.取出有效资料代入如何存入子表中<-----卡在这边
3.子表跟母表进行合并
4.返回GetDate()
请问该如何改asp.net下面的code?
Imports System.Data.SqlClient
Imports System.Data
Partial Class VBASPNETMenu
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
GenerateMenuItem()
End If
End Sub
Public Sub GenerateMenuItem()
' 从数据库中获取数据。
Dim ds As DataSet = GetData()
For Each mainRow As DataRow In ds.Tables(0).Rows
' 将主表中的记录加载到菜单控件。
Dim masterItem As New MenuItem(mainRow("mainName").ToString())
masterItem.NavigateUrl = mainRow("mainUrl").ToString()
Menu1.Items.Add(masterItem)
Menu1.Visible = True
For Each childRow As DataRow In mainRow.GetChildRows("Child")
' 根据主表和子表的关系,从子表中加载数据。
Dim childItem As New
MenuItem(DirectCast(childRow("childName"), String))
childItem.NavigateUrl = childRow("childUrl").ToString()
masterItem.ChildItems.Add(childItem)
Next
Next
End Sub
Public Function GetData() As DataSet
'SQL???回圈跑子表???
' 为了测试,使用内存表作为数据源。
Dim mainTB As New DataTable()
Dim mainIdCol As New DataColumn("mainId")
Dim mainNameCol As New DataColumn("mainName")
Dim mainUrlCol As New DataColumn("mainUrl")
mainTB.Columns.Add(mainIdCol)
mainTB.Columns.Add(mainNameCol)
mainTB.Columns.Add(mainUrlCol)
Dim childTB As New DataTable()
Dim childIdCol As New DataColumn("childId")
Dim childNameCol As New DataColumn("childName")
' 子表的MainId列是主表的外键。
Dim childMainIdCol As New DataColumn("MainId")
Dim childUrlCol As New DataColumn("childUrl")
childTB.Columns.Add(childIdCol)
childTB.Columns.Add(childNameCol)
childTB.Columns.Add(childMainIdCol)
childTB.Columns.Add(childUrlCol)
' 将一些测试记录插入到主表中。
Dim dr As DataRow = mainTB.NewRow()
dr(0) = "1"
dr(1) = "菜单管理"
dr(2) = "Test.aspx"
mainTB.Rows.Add(dr)
Dim dr1 As DataRow = mainTB.NewRow()
dr1(0) = "2"
dr1(1) = "新人训练"
dr1(2) = "Test.aspx"
mainTB.Rows.Add(dr1)
Dim dr2 As DataRow = mainTB.NewRow()
dr2(0) = "3"
dr2(1) = "薪俸发放"
dr2(2) = "Test.aspx"
mainTB.Rows.Add(dr2)
Dim dr3 As DataRow = mainTB.NewRow()
dr3(0) = "4"
dr3(1) = "食材管理"
dr3(2) = "Test.aspx"
mainTB.Rows.Add(dr3)
Dim dr4 As DataRow = mainTB.NewRow()
dr4(0) = "5"
dr4(1) = "人事管理"
dr4(2) = "Test.aspx"
mainTB.Rows.Add(dr4)
'将一些测试记录插入到子表中
Dim dr_child1 As DataRow = childTB.NewRow()
dr_child1(0) = "0"
dr_child1(1) = "1.T0101"
dr_child1(2) = "1"
dr_child1(3) = "Test.aspx"
childTB.Rows.Add(dr_child1)
Dim dr_child2 As DataRow = childTB.NewRow()
dr_child2(0) = "1"
dr_child2(1) = "2.T0103"
dr_child2(2) = "1"
dr_child2(3) = "Test.aspx"
childTB.Rows.Add(dr_child2)
Dim dr6 As DataRow = childTB.NewRow()
dr6(0) = "0"
dr6(1) = "T0201"
dr6(2) = "2"
dr6(3) = "Test.aspx"
childTB.Rows.Add(dr6)
Dim dr7 As DataRow = childTB.NewRow()
dr7(0) = "1"
dr7(1) = "T0202"
dr7(2) = "2"
dr7(3) = "Test.aspx"
childTB.Rows.Add(dr7)
' 使用DataSet来包含这两个表
Dim ds As New DataSet()
ds.Tables.Add(mainTB)
ds.Tables.Add(childTB)
' 建立主表和子表之间的关系
ds.Relations.Add("Child", ds.Tables(0).Columns("mainId"),
ds.Tables(1).Columns("MainId"))
Return ds
End Function
End Class
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 106.107.130.237
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1527508213.A.CFB.html
※ 编辑: qwerty147852 (106.107.130.237), 05/28/2018 19:52:54
1F:→ MOONRAKER: 为什麽你的imgur要开这麽久 05/28 22:26
2F:→ MOONRAKER: 你选的范例太难了 05/28 22:30
3F:→ MOONRAKER: 动态生子资料表出来 不要说你不会改 实务上没人这样弄 05/28 22:30
4F:→ MOONRAKER: 先找一个比较适合你的范例 不然教你太累 05/28 22:31