在VB.NET中使用Background Worker时遇到一个奇怪的问题: 我在表单中有以下代码:
Private Sub CalculateAccounts()
con.Open()
Dim SQLDelTrial As String = "DELETE TrialBalance.* FROM TrialBalance;"
Command = New OleDb.OleDbCommand(SQLDelTrial, con)
Command.ExecuteNonQuery()
con.Close()
Dim CountAccounts As String = "Select * From AccountLedger"
Dim DACountAccounts As New OleDbDataAdapter
Dim DSCountAccounts As New DataSet
DACountAccounts = New OleDbDataAdapter(CountAccounts, con)
DACountAccounts.Fill(DSCountAccounts, "DSCountAccountHere")
Dim Count As Long
For Count = 0 To DSCountAccounts.Tables(0).Rows.Count - 1
AccountCode = DSCountAccounts.Tables(0).Rows(Count).Item(0)
Dim AccountNormal As Integer
AccountNormal = DSCountAccounts.Tables(0).Rows(Count).Item(11)
If AccountNormal = 1 Then 'Debit Account
Call CalculateDebitAccount()
ElseIf AccountNormal = 2 Then 'Credit Account
Call CalculateCreditAccount()
End If
Next
Call UpdateDGV()
End Sub
Private Sub CalculateDebitAccount()
Dim TranConsDebitTotal As Decimal
Dim SQLDebitTotal As String = "SELECT TranConsolidated.DebitedAccID, Sum(TranConsolidated.DebitedAmount) AS SumOfDebitedAmount FROM(TranConsolidated)GROUP BY TranConsolidated.DebitedAccID HAVING (((TranConsolidated.DebitedAccID)=" & AccountCode & "));"
Dim DADebitTotal As New OleDbDataAdapter
Dim DSDebitTotal As New DataSet
DADebitTotal = New OleDbDataAdapter(SQLDebitTotal, con)
DADebitTotal.Fill(DSDebitTotal, "DSDebitTotalHere")
If DSDebitTotal.Tables(0).Rows.Count <> 0 Then
TranConsDebitTotal = DSDebitTotal.Tables(0).Rows(0).Item(1)
End If
Dim TranConsCreditTotal As Decimal
Dim SQLCreditTotal As String = "SELECT TranConsolidated.CreditedAccID, Sum(TranConsolidated.CreditedAmount) AS SumOfCreditedAmount FROM(TranConsolidated) GROUP BY TranConsolidated.CreditedAccID HAVING (((TranConsolidated.CreditedAccID)=" & AccountCode & "));"
Dim DACreditTotal As New OleDbDataAdapter
Dim DSCreditTotal As New DataSet
DACreditTotal = New OleDbDataAdapter(SQLCreditTotal, con)
DACreditTotal.Fill(DSCreditTotal, "DSCreditTotalHere")
If DSCreditTotal.Tables(0).Rows.Count <> 0 Then
TranConsCreditTotal = DSCreditTotal.Tables(0).Rows(0).Item(1)
End If
Dim SQLAccountOpening As String = "SELECT AccountLedger.ID, AccountLedger.AccOpBal FROM(AccountLedger) WHERE (((AccountLedger.ID)=" & AccountCode & "));"
Dim DAAccountOpening As New OleDbDataAdapter
Dim DSAccountOpening As New DataSet
DAAccountOpening = New OleDbDataAdapter(SQLAccountOpening, con)
DAAccountOpening.Fill(DSAccountOpening, "DSAccountOpeningHere")
Dim AccountNetBal As Decimal
If IsDBNull(DSAccountOpening.Tables(0).Rows(0).Item(1)) Then
AccountNetBal = TranConsDebitTotal - TranConsCreditTotal
Else
AccountNetBal = (Val(DSAccountOpening.Tables(0).Rows(0).Item(1)) + TranConsDebitTotal) - TranConsCreditTotal
End If
' MsgBox(AccountNetBal)
Dim SQLTrialBalance As String = "Select * From TrialBalance"
Dim DATrialBalance As New OleDbDataAdapter
Dim DSTrialBalance As New DataSet
Dim TrialNewRow As DataRow
DATrialBalance = New OleDbDataAdapter(SQLTrialBalance, con)
DATrialBalance.Fill(DSTrialBalance, "DSTrialBalanceHere")
If chkZeroBal.Checked = True Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
Else
If AccountNetBal <> 0 Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
End If
End If
Dim CBTrialBalance As New OleDbCommandBuilder(DATrialBalance)
DATrialBalance.Update(DSTrialBalance, "DSTrialBalanceHere")
End Sub
Private Sub CalculateCreditAccount()
Dim TranConsDebitTotal As Decimal
Dim SQLDebitTotal As String = "SELECT TranConsolidated.DebitedAccID, Sum(TranConsolidated.DebitedAmount) AS SumOfDebitedAmount FROM(TranConsolidated)GROUP BY TranConsolidated.DebitedAccID HAVING (((TranConsolidated.DebitedAccID)=" & AccountCode & "));"
Dim DADebitTotal As New OleDbDataAdapter
Dim DSDebitTotal As New DataSet
DADebitTotal = New OleDbDataAdapter(SQLDebitTotal, con)
DADebitTotal.Fill(DSDebitTotal, "DSDebitTotalHere")
If DSDebitTotal.Tables(0).Rows.Count <> 0 Then
TranConsDebitTotal = DSDebitTotal.Tables(0).Rows(0).Item(1)
End If
Dim TranConsCreditTotal As Decimal
Dim SQLCreditTotal As String = "SELECT TranConsolidated.CreditedAccID, Sum(TranConsolidated.CreditedAmount) AS SumOfCreditedAmount FROM(TranConsolidated) GROUP BY TranConsolidated.CreditedAccID HAVING (((TranConsolidated.CreditedAccID)=" & AccountCode & "));"
Dim DACreditTotal As New OleDbDataAdapter
Dim DSCreditTotal As New DataSet
DACreditTotal = New OleDbDataAdapter(SQLCreditTotal, con)
DACreditTotal.Fill(DSCreditTotal, "DSCreditTotalHere")
If DSCreditTotal.Tables(0).Rows.Count <> 0 Then
TranConsCreditTotal = DSCreditTotal.Tables(0).Rows(0).Item(1)
End If
Dim SQLAccountOpening As String = "SELECT AccountLedger.ID, AccountLedger.AccOpBal FROM(AccountLedger) WHERE (((AccountLedger.ID)=" & AccountCode & "));"
Dim DAAccountOpening As New OleDbDataAdapter
Dim DSAccountOpening As New DataSet
DAAccountOpening = New OleDbDataAdapter(SQLAccountOpening, con)
DAAccountOpening.Fill(DSAccountOpening, "DSAccountOpeningHere")
Dim AccountNetBal As Decimal
If IsDBNull(DSAccountOpening.Tables(0).Rows(0).Item(1)) Then
AccountNetBal = TranConsCreditTotal - TranConsDebitTotal
Else
AccountNetBal = (Val(DSAccountOpening.Tables(0).Rows(0).Item(1)) + TranConsCreditTotal) - TranConsDebitTotal
End If
' MsgBox(AccountNetBal)
Dim SQLTrialBalance As String = "Select * From TrialBalance"
Dim DATrialBalance As New OleDbDataAdapter
Dim DSTrialBalance As New DataSet
Dim TrialNewRow As DataRow
DATrialBalance = New OleDbDataAdapter(SQLTrialBalance, con)
DATrialBalance.Fill(DSTrialBalance, "DSTrialBalanceHere")
If chkZeroBal.Checked = True Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
Else
If AccountNetBal <> 0 Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
End If
End If
Dim CBTrialBalance As New OleDbCommandBuilder(DATrialBalance)
DATrialBalance.Update(DSTrialBalance, "DSTrialBalanceHere")
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim CountAccounts As String = "Select * From AccountLedger"
Dim DACountAccounts As New OleDbDataAdapter
Dim DSCountAccounts As New DataSet
DACountAccounts = New OleDbDataAdapter(CountAccounts, Con)
DACountAccounts.Fill(DSCountAccounts, "DSCountAccountHere")
Dim Count As Long
For Count = 0 To DSCountAccounts.Tables(0).Rows.Count - 1
AccountCode = DSCountAccounts.Tables(0).Rows(Count).Item(0)
Dim AccountNormal As Integer = DSCountAccounts.Tables(0).Rows(Count).Item(11)
If AccountNormal = 1 Then 'Debit Account
' Call CalculateDebitAccount()
Dim TranConsDebitTotal As Decimal
Dim SQLDebitTotal As String = "SELECT TranConsolidated.DebitedAccID, Sum(TranConsolidated.DebitedAmount) AS SumOfDebitedAmount FROM(TranConsolidated)GROUP BY TranConsolidated.DebitedAccID HAVING (((TranConsolidated.DebitedAccID)=" & AccountCode & "));"
Dim DADebitTotal As New OleDbDataAdapter
Dim DSDebitTotal As New DataSet
DADebitTotal = New OleDbDataAdapter(SQLDebitTotal, Con)
DADebitTotal.Fill(DSDebitTotal, "DSDebitTotalHere")
If DSDebitTotal.Tables(0).Rows.Count <> 0 Then
TranConsDebitTotal = DSDebitTotal.Tables(0).Rows(0).Item(1)
End If
Dim TranConsCreditTotal As Decimal
Dim SQLCreditTotal As String = "SELECT TranConsolidated.CreditedAccID, Sum(TranConsolidated.CreditedAmount) AS SumOfCreditedAmount FROM(TranConsolidated) GROUP BY TranConsolidated.CreditedAccID HAVING (((TranConsolidated.CreditedAccID)=" & AccountCode & "));"
Dim DACreditTotal As New OleDbDataAdapter
Dim DSCreditTotal As New DataSet
DACreditTotal = New OleDbDataAdapter(SQLCreditTotal, Con)
DACreditTotal.Fill(DSCreditTotal, "DSCreditTotalHere")
If DSCreditTotal.Tables(0).Rows.Count <> 0 Then
TranConsCreditTotal = DSCreditTotal.Tables(0).Rows(0).Item(1)
End If
Dim SQLAccountOpening As String = "SELECT AccountLedger.ID, AccountLedger.AccOpBal FROM(AccountLedger) WHERE (((AccountLedger.ID)=" & AccountCode & "));"
Dim DAAccountOpening As New OleDbDataAdapter
Dim DSAccountOpening As New DataSet
DAAccountOpening = New OleDbDataAdapter(SQLAccountOpening, Con)
DAAccountOpening.Fill(DSAccountOpening, "DSAccountOpeningHere")
Dim AccountNetBal As Decimal
If IsDBNull(DSAccountOpening.Tables(0).Rows(0).Item(1)) Then
AccountNetBal = TranConsDebitTotal - TranConsCreditTotal
Else
AccountNetBal = (Val(DSAccountOpening.Tables(0).Rows(0).Item(1)) + TranConsDebitTotal) - TranConsCreditTotal
End If
' MsgBox(AccountNetBal)
Dim SQLTrialBalance As String = "Select * From TrialBalance"
Dim DATrialBalance As New OleDbDataAdapter
Dim DSTrialBalance As New DataSet
Dim TrialNewRow As DataRow
DATrialBalance = New OleDbDataAdapter(SQLTrialBalance, Con)
DATrialBalance.Fill(DSTrialBalance, "DSTrialBalanceHere")
If chkZeroBal.Checked = True Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
Else
If AccountNetBal <> 0 Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
End If
End If
Dim CBTrialBalance As New OleDbCommandBuilder(DATrialBalance)
DATrialBalance.Update(DSTrialBalance, "DSTrialBalanceHere")
ElseIf AccountNormal = 2 Then 'Credit Account
'Call CalculateCreditAccount()
Dim TranConsDebitTotal As Decimal
Dim SQLDebitTotal As String = "SELECT TranConsolidated.DebitedAccID, Sum(TranConsolidated.DebitedAmount) AS SumOfDebitedAmount FROM(TranConsolidated)GROUP BY TranConsolidated.DebitedAccID HAVING (((TranConsolidated.DebitedAccID)=" & AccountCode & "));"
Dim DADebitTotal As New OleDbDataAdapter
Dim DSDebitTotal As New DataSet
DADebitTotal = New OleDbDataAdapter(SQLDebitTotal, Con)
DADebitTotal.Fill(DSDebitTotal, "DSDebitTotalHere")
If DSDebitTotal.Tables(0).Rows.Count <> 0 Then
TranConsDebitTotal = DSDebitTotal.Tables(0).Rows(0).Item(1)
End If
Dim TranConsCreditTotal As Decimal
Dim SQLCreditTotal As String = "SELECT TranConsolidated.CreditedAccID, Sum(TranConsolidated.CreditedAmount) AS SumOfCreditedAmount FROM(TranConsolidated) GROUP BY TranConsolidated.CreditedAccID HAVING (((TranConsolidated.CreditedAccID)=" & AccountCode & "));"
Dim DACreditTotal As New OleDbDataAdapter
Dim DSCreditTotal As New DataSet
DACreditTotal = New OleDbDataAdapter(SQLCreditTotal, Con)
DACreditTotal.Fill(DSCreditTotal, "DSCreditTotalHere")
If DSCreditTotal.Tables(0).Rows.Count <> 0 Then
TranConsCreditTotal = DSCreditTotal.Tables(0).Rows(0).Item(1)
End If
Dim SQLAccountOpening As String = "SELECT AccountLedger.ID, AccountLedger.AccOpBal FROM(AccountLedger) WHERE (((AccountLedger.ID)=" & AccountCode & "));"
Dim DAAccountOpening As New OleDbDataAdapter
Dim DSAccountOpening As New DataSet
DAAccountOpening = New OleDbDataAdapter(SQLAccountOpening, Con)
DAAccountOpening.Fill(DSAccountOpening, "DSAccountOpeningHere")
Dim AccountNetBal As Decimal
If IsDBNull(DSAccountOpening.Tables(0).Rows(0).Item(1)) Then
AccountNetBal = TranConsCreditTotal - TranConsDebitTotal
Else
AccountNetBal = (Val(DSAccountOpening.Tables(0).Rows(0).Item(1)) + TranConsCreditTotal) - TranConsDebitTotal
End If
' MsgBox(AccountNetBal)
Dim SQLTrialBalance As String = "Select * From TrialBalance"
Dim DATrialBalance As New OleDbDataAdapter
Dim DSTrialBalance As New DataSet
Dim TrialNewRow As DataRow
DATrialBalance = New OleDbDataAdapter(SQLTrialBalance, Con)
DATrialBalance.Fill(DSTrialBalance, "DSTrialBalanceHere")
If chkZeroBal.Checked = True Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
Else
If AccountNetBal <> 0 Then
TrialNewRow = DSTrialBalance.Tables(0).NewRow()
If AccountNetBal < 0 Then
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(1) = Math.Abs(AccountNetBal)
TrialNewRow.Item(3) = 0
Else
TrialNewRow.Item(0) = AccountCode
TrialNewRow.Item(2) = AccountNetBal
TrialNewRow.Item(3) = 1
End If
DSTrialBalance.Tables(0).Rows.Add(TrialNewRow)
End If
End If
Dim CBTrialBalance As New OleDbCommandBuilder(DATrialBalance)
DATrialBalance.Update(DSTrialBalance, "DSTrialBalanceHere")
End If
Next
System.Threading.Thread.Sleep(1000)
BackgroundWorker1.ReportProgress(AccountCode)
' Call UpdateDGV()
End Sub
当我在表单中运行前三个Subs时,计算结果是准确的。但是,在backgroundworker中运行接近相似的代码会返回完全错误的结果。 在后台工作者编码时出了什么问题?
哈立德。