假设做一个ERP的库存查询系统。表有2个:库存表icinventory(FItemID、fstockid、fqty),基础资料表t_item(包含仓位和产品表:fitemid、fitemclassid、flevel、fnumber、fname、FparentID、fdetail),具体字段含义暂不做解释,有时结合数据应该可以猜出来的。
表中建立子窗体,使用5种控件:SplitContainer、TreeView、DataList、Button、Label做ERP的库存查询系统。另外使用一个全局变量vars("item")
充分利用SQLCommand的cmd.ExecuteReader()读外部数据源(可以跨表操作,之后还可以进一步跨数据源操作)、写树节点、数据列表的功能。
按钮一:按产品查询
Dim tv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim td As WinForm.TreeNode
tv.Nodes.clear
Dim cmd As New SQLCommand
Dim dt As DataTable
cmd.C
cmd.CommandText ="SELECT * FROM t_item where fitemclassid=4 and flevel =1"
dt = cmd.ExecuteReader() \'生成统计表
For Each dr As Datarow In dt.Datarows
td=tv.Nodes.Add( dr("FItemID"), dr("fnumber") & ":" & dr("fname"),"home.ico")
Next
vars("item")="cp"
Dim dst As WinForm.DataList = e.Form.Controls("DataList1")
dst.DataTable = dt
dst.RowFilter = false
dst.Build()
按钮二:按仓位查询
Dim tv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim td As WinForm.TreeNode
tv.Nodes.clear
Dim cmd As New SQLCommand
Dim dt As DataTable
cmd.C
cmd.CommandText ="SELECT * FROM t_item where fitemclassid=5 and flevel =1"
dt = cmd.ExecuteReader() \'生成统计表
For Each dr As Datarow In dt.Datarows
td=tv.Nodes.Add( dr("FItemID"), dr("fnumber") & ":" & dr("fname"),"home.ico")
Next
vars("item")="ck"
Dim dst As WinForm.DataList = e.Form.Controls("DataList1")
dst.RowFilter = false
dst.Build()
目录树控件的 AfterSelectNode事件代码:(以下代码框架简单,只是结合数据库的SELECT语句有所不同而已。)
dim itemk3 as string
Select Case vars("item")
Case "cp"
itemk3 ="b.fitemid"
Case "ck"
itemk3 ="b.fstockid"
Case Else
End Select
Dim cmd As New SQLCommand
Dim dt As DataTable
cmd.C
cmd.CommandText ="SELECT * FROM t_item where FparentID=" & e.node.name & " and (fdetail=0 or (fdetail=1 and FItemID in (SELECT a.FItemID FROM t_item as a INNER JOIN icinventory as b ON a.fitemid=" & itemk3 & " where b.fqty <> 0 and a.FparentID=" & e.node.name & " ))) "
dt = cmd.ExecuteReader() \'生成统计表
e.Node.Nodes.Clear
dim level0 as integer
For Each dr As DataRow In dt.Datarows
if not dr("fdetail") then
e.Node.Nodes.Add( dr("FItemID"),dr("fnumber") & " " & dr("fname"),"home.ico")
else
e.Node.Nodes.Add( dr("FItemID"),dr("fnumber") & " " & dr("fname"),"Accept.ico")
end if
next
dim selt1,selt2,selt3 as string
Select Case vars("item")
Case "cp"
selt1="SELECT a.fnumber AS 产品代码, a.fname AS 产品型号, c.fnumber AS 仓库代码, c.fname AS 仓库名称, b.fqty AS 库存数量 "
selt2="FROM (t_item AS a INNER JOIN icinventory AS b ON a.fitemid=" & itemk3 & ") INNER JOIN t_item AS c ON b.fstockid=c.fitemid "
Case "ck"
selt1="SELECT a.fnumber AS 仓库代码, a.fname AS 仓库名称, c.fnumber AS 产品代码, c.fname AS 产品型号, b.fqty AS 库存数量 "
selt2="FROM (t_item AS a INNER JOIN icinventory AS b ON a.fitemid=" & itemk3 & ") INNER JOIN t_item AS c ON b.fitemid=c.fitemid "
Case Else
End Select
selt3="where a.flevel>1 and a.fdetail=1 and b.fqty > 0 and a.fparentid=" & e.node.name & " or (a.FItemID=" & e.node.name & " and b.fqty > 0) "
cmd.CommandText =selt1 & selt2 & selt3
dt = cmd.ExecuteReader() \'生成统计表
Dim dst As WinForm.DataList = e.Form.Controls("DataList1")
dst.DataTable = dt
dst.Build()