1

我在这里有代码,如果每行数据没有更新,我想在文本文件中写入错误消息。我能知道我的 try..catch 块是对还是错?

        For rCnt = 3 To 4
                Empno = xlRange.Cells(rCnt, 1).Value
                totalhrs = xlRange.Cells(rCnt, 3).Value 'workhr
                latehr = xlRange.Cells(rCnt, 4).Value 'latehr
                earlyhr = xlRange.Cells(rCnt, 5).Value 'earlyhr
                norOT = xlRange.Cells(rCnt, 6).Value 'nor OT
                holOT = xlRange.Cells(rCnt, 7).Value 'hol ot
                otherOT = xlRange.Cells(rCnt, 8).Value 'other ot
                attend = xlRange.Cells(rCnt, 9).Value 'attendace
                absent = xlRange.Cells(rCnt, 10).Value 'absent
                mc = xlRange.Cells(rCnt, 11).Value 'leave

                sCommand = "UPDATE paytran.dbf SET paytran.workhr = " & totalhrs & ", paytran.latehr = " & latehr & ", paytran.earlyhr = " & earlyhr & ", paytran.ot1 = " & norOT & ",  paytran.ot2 = " & holOT & ",  paytran.ot3 = " & otherOT & ", paytran.dw = " & attend & ", paytran.ab = " & absent & ", paytran.mc = " & mc & " , paytran.payyes = 'Y' WHERE paytran.empno == '" & Empno & "'"
                dBaseCommand = New OleDbCommand(sCommand, dBaseConnection)

                Try
                    retVal = dBaseCommand.ExecuteNonQuery()

                Catch ex As IOException
                    'To_WriteAudit("Error", ex.Message.ToString()) 'here my catch block that write the file..
                End Try
            Next rCnt


Public Sub To_WriteAudit(ByVal pstrAction As String, ByVal pstrErrorDes As String)

    Dim pathlf As String

    pathlf = "C:\logfile.txt"

    Dim fswAuditLog As StreamWriter
    fswAuditLog = New StreamWriter(pathlf, True)
    fswAuditLog.WriteLine("Write the exception here")
    fswAuditLog.Flush()
    fswAuditLog.Close()

End Sub
4

1 回答 1

1

Try-Catch 块是正确的,如果更新执行中出现错误(未注释时),则应调用 to_writeaudit。但是,您还应该测试 retVal 以确保它为 1(更新的行数)。如果为零,则不更新任何内容。如果它大于零,则更新了不止一行。

此外,您可能希望将 sCommand 和 dBaseCommand 的分配放在 Try-Catch 块中,以防它们产生错误。

于 2013-07-05T03:07:51.677 回答