如何防止数组一起添加(sum)

时间:2014-09-18 16:23:24

标签: arrays vb.net

我有一个数组。我的程序通过并设置数组。但是,当我显示数组时,它会将数组添加到一起。

这是我的代码:

Public Class frmMain
Dim connetionString As String 
Dim connection As SqlConnection 
Dim command As SqlCommand 
Dim adapter As New SqlDataAdapter 
Dim ds As New DataSet 
Dim sql As String 
Dim yPoint As Integer
Dim LocationDB As String
Dim dtstartdate As Date
Dim dtenddate As Date
Dim LocationName As String
Dim BookSales(17) As Integer 
Public Shared locationcounter As Integer

Dim i As Integer





Public Sub Get_Info()



    If locationcounter < 18 Then

        dtstartdate = dtpStartDate.Value
        dtenddate = dtpEndDate.Value.AddDays(1).AddSeconds(-1)


        Try
            connetionString = "Data Source=" & LocationDB & ";Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=test"
            sql = "Select * from fGetdata"
            connection = New SqlConnection(connetionString)
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.SelectCommand.CommandTimeout = 130
            adapter.SelectCommand.Parameters.AddWithValue("@StartDate", dtstartdate)
            adapter.SelectCommand.Parameters.AddWithValue("@EndDate", dtenddate)
            adapter.Fill(ds)
            connection.Close()
            connection.Dispose()

        Catch ex As Exception
            MsgBox(ex.Message)


        End Try
        For Each FoundRow As DataRow In ds.Tables(0).Rows
            Select Case FoundRow("CategoryName")
                Case "TOTAL"
                    Select Case FoundRow("Description")
                        Case "BOOK", "BOOK SALES", "GC"
                            BookSales(i) = BookSales(i) + (FoundRow("netAmt"))
                    End Select

            End Select

        Next
        MsgBox(LocationName & BookSales(i))
        MsgBox(LocationName & BookSales(0))
        MsgBox(LocationName & BookSales(1))


    End If


End Sub

Public Sub GetLocation()
        Select Case locationcounter
            Case "1"
                LocationName = "Location1"
                Locationdb = "10.0.1.52"
            Case "2"
                LocationName = "Location2"
                Locationdb = "10.0.1.51"
            Case "3"
                LocationName = "Location3"
                Locationdb = "10.0.1.50"

        End Select
    End Sub

按钮点击:

For x = 1 To 3

            GetLocation()
            Label1.Text = LocationName
            Label1.Refresh()
            Get_Info()
            i = i + 1
            locationcounter = locationcounter + 1
        Next

我得到了:

Location1 5
Location2 25
Location3 35

我想得到:

Location1 5
Location2 20
Location3 10

由于某种原因,阵列正在加在一起

1 个答案:

答案 0 :(得分:1)

正如您所指出的,问题是DataSet被重用,因此它在每个循环中累积了结果。

您需要清理编码风格。把事情放在尽可能紧的范围内。在这种情况下,应该在方法中声明Get_Info()方法使用的所有变量。这可以防止长生命变量的副作用。 DataSet仅在Get_Info方法中使用,因此它应该只存在于那里。

清理Finally块中的资源。在下面的示例中,我将connection.Dispose移动到finally块中。你只需要调用Dispose,你也不需要关闭。

您还应该启用Option Strict和Option Explicit。这些将有助于防止在运行时之前未显示的套管错误。作为类型不匹配的示例,您将loctioncounter声明为整数,但在GetLocation方法中将其用作字符串。

还有更多,但这应该让你开始朝着正确的方向前进。

Public Class frmMain
Dim yPoint As Integer
Dim LocationDB As String
Dim dtstartdate As Date
Dim dtenddate As Date
Dim LocationName As String
Dim BookSales(17) As Integer
Public Shared locationcounter As Integer
Dim i As Integer

Public Sub Get_Info()
    Dim connetionString As String
    Dim connection As SqlConnection = Nothing
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    If locationcounter < 18 Then

        dtstartdate = dtpStartDate.Value
        dtenddate = dtpEndDate.Value.AddDays(1).AddSeconds(-1)
        Try
            connetionString = "Data Source=" & LocationDB & ";Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=test"
            sql = "Select * from fGetdata"
            connection = New SqlConnection(connetionString)
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.SelectCommand.CommandTimeout = 130
            adapter.SelectCommand.Parameters.AddWithValue("@StartDate", dtstartdate)
            adapter.SelectCommand.Parameters.AddWithValue("@EndDate", dtenddate)
            adapter.Fill(ds)

            For Each FoundRow As DataRow In ds.Tables(0).Rows
                Select Case FoundRow("CategoryName")
                    Case "TOTAL"
                        Select Case FoundRow("Description")
                            Case "BOOK", "BOOK SALES", "GC"
                                BookSales(i) = BookSales(i) + (FoundRow("netAmt"))
                        End Select

                End Select

            Next

            MsgBox(LocationName & BookSales(i))
            MsgBox(LocationName & BookSales(0))
            MsgBox(LocationName & BookSales(1))


        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            If connection IsNot Nothing Then
                connection.Dispose()
            End If
        End Try
    End If
End Sub


Public Sub GetLocation()
    Select Case locationcounter
        Case "1"
            LocationName = "Location1"
            LocationDB = "10.0.1.52"
        Case "2"
            LocationName = "Location2"
            LocationDB = "10.0.1.51"
        Case "3"
            LocationName = "Location3"
            LocationDB = "10.0.1.50"

    End Select
End Sub