存储过程的树视图

时间:2015-05-12 15:08:47

标签: sql-server vb.net stored-procedures treeview

我有一个树视图,我试图从存储过程填充,我似乎无法得到我想要的结果。

存储过程如下所示:

ALTER PROCEDURE [dbo].[Inspections_LoadRecords]
    @SearchValue varchar(30) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT      
        InspectorName.InspectorName,
        InspectionHeader.InspectionDate,
        PropertyMaster.AddressLine1,
        InspectionProperties.PropertyID,
        InspectionHeader.InspectionID,
        InspectorName.InspectorID, 
        InspectionProperties.GroupID
    FROM            
        InspectionProperties 
    LEFT OUTER JOIN
        InspectionHeader ON InspectionProperties.InspectionID = InspectionHeader.InspectionID 
    LEFT OUTER JOIN
        InspectorName ON InspectionHeader.InspectorID = InspectorName.InspectorID 
                      AND InspectionHeader.InspectorID = InspectorName.InspectorID 
    LEFT OUTER JOIN
        PropertyMaster ON InspectionProperties.PropertyID = PropertyMaster.PropertyID
    WHERE       
        (InspectionHeader.InspectionID = InspectionHeader.InspectionID)
    ORDER BY  
        InspectorName.InspectorName DESC
END

然后加载treeview的代码

   Sub LoadInspectionTree()
    '**Loads Property List
    ' Initialise Error Checking


    ' Dimension Local Variables
    Dim uRecSnap As ADODB.Recordset
    Dim uPar As ADODB.Parameter
    Dim uNode As TreeNode
    Dim aNode As TreeNode
    Dim bNode As TreeNode
    Dim iInspectorID As Integer = 0
    Dim iInspectorcnt As Integer = 0
    Dim iInspectionID As Integer = 0
    Dim iInspectionIDcnt As Integer = 0
    Dim iGroupNumber As Integer = 0
    Dim iGroupNumbercnt = 0
    Dim uStackframe As New Diagnostics.StackFrame
    Try

        ' Check For Open Connection
        If uDBase Is Nothing Then
            OpenConnection()
            bConnection = True
        End If

        ' Run Stored Procedure - Load Property List (Based on Search Value)
        uCommand = New ADODB.Command
        With uCommand
            .ActiveConnection = uDBase
            .CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
            .CommandTimeout = 0
            uPar = .CreateParameter("@SearchValue", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 30)
            .Parameters.Append(uPar)
            .Parameters("@SearchValue").Value = txtFilter.Text
            .CommandText = "Inspections_LoadRecords"
            uRecSnap = .Execute
        End With


        ' Suppress TreeView Repaint / Clear TreeView
        tgvInspections.BeginUpdate()
        tgvInspections.Nodes.Clear()
        tgvInspections.ShowNodeToolTips = True

        ' Populate List


        Do Until uRecSnap.EOF

            If iInspectorID <> uRecSnap("InspectorID").Value Then
                uNode = tgvInspections.Nodes.Add("N" & Format(uRecSnap("InspectorID").Value, "0000"), uRecSnap("InspectorName").Value)
                uNode.Tag = "N:" & Format(uRecSnap("InspectorID").Value, "0000") & ":0000" & Format(uRecSnap("InspectionID").Value, "0000") & "0000"
                iInspectorID = uRecSnap("InspectorID").Value
                iInspectorcnt = iInspectorcnt + 1

            End If

            If (IsDBNull(uRecSnap("GroupID").Value)) = False Then
                If iGroupNumber <> uRecSnap("GroupID").Value Then
                    aNode = uNode.Nodes.Add("I" & Format(uRecSnap("GroupID").Value, "0000"), uRecSnap("InspectionDate").Value)
                    aNode.Tag = "I:" & Format(uRecSnap("InspectionDate").Value, "") & Format(uRecSnap("InspectorID").Value, "0000") & Format(uRecSnap("InspectionID").Value, "0000") & Format(uRecSnap("GroupID").Value, "0000")
                    iGroupNumber = uRecSnap("GroupID").Value
                    iGroupNumbercnt = iGroupNumbercnt + 1


                End If


                bNode = uNode.Nodes(iGroupNumbercnt - 1).Nodes.Add("P" & Format(uRecSnap("GroupID").Value, "0000"), uRecSnap("AddressLine1").Value)
                bNode.Tag = "P:" & Format(uRecSnap("InspectionID").Value, "0000") & Format(uRecSnap("PropertyID").Value, "0000") & "0000" & Format(uRecSnap("InspectorID").Value, "0000") & Format(uRecSnap("GroupID").Value, "0000")
                uRecSnap.MoveNext()

            End If
            ' End If
        Loop

        Do Until uRecSnap.EOF

        Loop
        uRecSnap.Close()


        ' Close Connection


    Catch ex As Exception
        ' Catch Error
        If Err.Number <> 0 Then
            WriteAuditLogRecord(uStackframe.GetMethod.DeclaringType.FullName, uStackframe.GetMethod.Name.ToString, "Error", Err.Description & vbCrLf & vbCrLf & ex.StackTrace, 0)
            MsgBox("System Error Ref: " & sAuditID & vbCrLf & uStackframe.GetMethod.DeclaringType.FullName & " / " & uStackframe.GetMethod.Name.ToString & vbCrLf & Err.Description & vbCrLf & vbCrLf & ex.StackTrace & Chr(13) & sErrDescription, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Business Management System - Unexepected Error Ref: " & sAuditID)
        End If

    Finally

        If bConnection Then CloseConnection()
        uRecSnap = Nothing

    End Try
End Sub

我即将到来的树视图是

Richard Glass -
              --2015-05-11
                           --123 Fake Street
                           --124 Fake Street
              --2015-05-12
                           -- 122 Fake Street
                            -- 1234 Fake Street
Molly McEwan--
              --2015-05-13

在此记录之后,当应该有另一个地址时它会失败

任何想法都会有所帮助。

存储过程返回

Inspector      Date    Address, PropID,  InsapecionID ,InspectorID, GroupID
RichardGlass---2015-05-11----123 Fake Street----23----1----1----1
RichardGlass---2015-05-11----124 Fake Street---24-----2----1----1
RichardGlass---2015-05-12----122 Fake Street-- 25-----3----1----2
RichardGlass---2015-05-12----132 Fake Street---26-----4----1----2
MollyMcEwan ---2015-05-13----145 Fake Street --27-----5----3----3

2 个答案:

答案 0 :(得分:0)

当你说“失败”时,目前尚不清楚......失败意味着什么?

首先,我会确保查询返回正确的值。就像@Sean所说,你做的是1 = 1.

我不知道GroupID是什么,但由于您的节点是名称和日期,因此您应该通过InspectorName和InspectionDate进行排序。

你的变量名有点令人困惑,我会

Dim nameNode, dateNode, addrNode As TreeNode

我也会稍微改变一下逻辑

Do Until uRecSnap.EOF
    Dim currentNameTag, currentDateTag As String

    currentNameTag = Format(uRecSnap("InspectorID").Value, "0000")
    currentDateTag = Format(uRecSnap("InspectionDate").Value, "")

    If nameNode Is Nothing OrElse nameNode.Tag <> currentNameTag Then
        ' Create a new nameNode and add it to tgvInspections.Nodes
        dateNode = Nothing
    End If

    If dateNode Is Nothing OrElse dateNode.Tag <> currentDateTag Then
        ' Create a new nameNode and add it to nameNode
    End If

    ' Create a new addrNode and add it to dateNode

    uRecSnap.MoveNext()
Loop

答案 1 :(得分:0)

我解决了我遇到的问题我在添加每个新的Inspector后没有重置IGroupCount,所以当计数关闭后添加地址失败了

                If iInspectorID <> uRecSnap("InspectorID").Value Then
                uNode = tgvInspections.Nodes.Add("N" & Format(uRecSnap("InspectorID").Value, "0000"), uRecSnap("InspectorName").Value)
                uNode.Tag = "N:" & Format(uRecSnap("InspectorID").Value, "0000") & ":0000" & Format(uRecSnap("InspectionID").Value, "0000") & "0000"
                iInspectorID = uRecSnap("InspectorID").Value
                iGroupNumbercnt = 0
            End If

            If (IsDBNull(uRecSnap("GroupID").Value)) = False Then
                If iGroupNumber <> uRecSnap("GroupID").Value Then
                    aNode = uNode.Nodes.Add("I" & Format(uRecSnap("GroupID").Value, "0000"), uRecSnap("InspectionDate").Value)
                    aNode.Tag = "I:" & Format(uRecSnap("InspectionDate").Value, "") & Format(uRecSnap("InspectorID").Value, "0000") & Format(uRecSnap("InspectionID").Value, "0000") & Format(uRecSnap("GroupID").Value, "0000")
                    iGroupNumber = uRecSnap("GroupID").Value
                    iGroupNumbercnt = iGroupNumbercnt + 1

                End If

                bNode = uNode.Nodes(iGroupNumbercnt - 1).Nodes.Add("P" & Format(uRecSnap("GroupID").Value, "0000"), uRecSnap("AddressLine1").Value)
                bNode.Tag = "P:" & Format(uRecSnap("InspectionID").Value, "0000") & Format(uRecSnap("PropertyID").Value, "0000") & "0000" & Format(uRecSnap("InspectorID").Value, "0000") & Format(uRecSnap("GroupID").Value, "0000")

                uRecSnap.MoveNext()
            End If

            ' End If
        Loop

    Do Until uRecSnap.EOF