使用MS ACCESS数据库在VB.net上执行Reader错误

时间:2017-06-30 02:54:07

标签: database vb.net ms-access

我只想询问我应该在哪里放置执行阅读器,这样我就可以编写一个代码,当函数成功运行时会弹出一条消息。这是代码。

    xprovider = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\Users\Public\Downloads\dbInventoryManagementProgram.accdb")
    xconString = xprovider
    xmyConnection.ConnectionString = xconString
    xmyConnection.Open()

    Dim xstr As String
    xstr = "Insert into tblReports([Item],[Brand],[Quantity],[Transaction_Type],[Transaction_Date]) Values (?,?,?,?,?)"
    Dim xcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(xstr, xmyConnection)
    'Dim xdr As OleDbDataReader = xcmd.ExecuteReader()
    xcmd.Parameters.Add(New OleDb.OleDbParameter("Item", CType(txtboxItem.Text, String)))
    xcmd.Parameters.Add(New OleDb.OleDbParameter("Brand", CType(txtboxBrand.Text, String)))
    xcmd.Parameters.Add(New OleDb.OleDbParameter("Quantity", CType(txtboxQuantity.Text, String)))
    xcmd.Parameters.Add(New OleDb.OleDbParameter("Transaction_Type", CType(txtboxTransactionType.Text, String)))
    xcmd.Parameters.Add(New OleDb.OleDbParameter("Transaction_Date", CType(dtpTransactionDate.Text, String)))

    Try
        xcmd.ExecuteNonQuery()
        xcmd.Dispose()
        xmyConnection.Close()
        txtboxTransactionType.Clear()
    Catch ex As Exception
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
    End Try

尝试             如果txtboxItem.Text =“”那么                 MsgBox(“警告!不要将任何字段留空。请再试一次。”,MsgBoxStyle.Critical)                 txtboxItem.Clear()                 txtboxBrand.Clear()                 txtboxQuantity.Clear()             ElseIf txtboxBrand.Text =“”然后                 MsgBox(“警告!不要将任何字段留空。请再试一次。”,MsgBoxStyle.Critical)                 txtboxItem.Clear()                 txtboxBrand.Clear()                 txtboxQuantity.Clear()             ElseIf txtboxQuantity.Text =“”然后                 MsgBox(“警告!不要将任何字段留空。请再试一次。”,MsgBoxStyle.Critical)                 txtboxItem.Clear()                 txtboxBrand.Clear()                 txtboxQuantity.Clear()             ElseIf dtpTransactionDate.Text =“”然后                 MsgBox(“警告!不要将任何字段留空。请再试一次。”,MsgBoxStyle.Critical)                 txtboxItem.Clear()                 txtboxBrand.Clear()                 txtboxQuantity.Clear()             ElseIf xdr.HasRows然后                 MsgBox(“输入成功!按ok继续....”,MsgBoxStyle.Information)                 txtboxItem.Clear()                 txtboxBrand.Clear()                 txtboxQuantity.Clear()             其他                 MsgBox(“警告!检测到一些错误。请正确填写表格。”,MsgBoxStyle.Critical)             万一         赶上前例外             MsgBox(“错误:”& ex.Message,MsgBoxStyle.Critical)         结束尝试

2 个答案:

答案 0 :(得分:0)

您可以像这样使用Try / Catch中的Finally块。你应该把你的连接放在Try中。一切都应该“尝试”和“抓住”。对用户好。不要让他们看到他们不理解的不必要的错误或消息。在他们可以发送给您的文件中记录错误详细信息,并为他们保留简单的信息。

您需要捕获特定的异常,以便了解如何解决每个异常。并非所有错误都需要通知用户。你或许可以恢复并再次尝试。

Try
    xcmd.ExecuteNonQuery()
    xcmd.Dispose()
    xmyConnection.Close()
    txtboxTransactionType.Clear()
Catch ex As Exception
    MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
Finally
    MsgBox("Process Completed")
End Try

我重读了你的问题。要成功发送消息,只需将msgbox移动到Try。

Try
    xcmd.ExecuteNonQuery()
    xcmd.Dispose()
    xmyConnection.Close()
    txtboxTransactionType.Clear()
    MsgBox("Process Completed Sucessfully")
Catch ex As Exception
    MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
Finally
    'Cleanup
    'any lines of code after the errored line will not execute,
    'thus you might leave things hanging around not disposed of properly.
    'The Finally block will always execute, error or not.
End Try

答案 1 :(得分:0)

正如您所要求的,以下是我可能会对您的代码执行的一些操作。我只能基于您提供的代码。我跳过了你的无格式代码。我没有测试这段代码。使用此作为如何编写代码的示例。几乎所有的代码都应该包含在Try / Catch中。只有在必要时才会通知用户。很多时候,只要知道错误是什么,就可以从错误中恢复。当您必须通知用户时,请保持简单并将详细信息记录在他们可以发送给您的文件中。

    Try
    xprovider = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\Users\Public\Downloads\dbInventoryManagementProgram.accdb")
    xconString = xprovider
    'I don't know where you create this so I don't know you connection class
    Using xmyConnection As New WhatEverClassThisIs
        xmyConnection.ConnectionString = xconString
        xmyConnection.Open()

        Dim xstr As String
        xstr = "Insert into tblReports([Item],[Brand],[Quantity],[Transaction_Type],[Transaction_Date]) Values (?,?,?,?,?)"
        Using xcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(xstr, xmyConnection)
            'Dim xdr As OleDbDataReader = xcmd.ExecuteReader()
            xcmd.Parameters.Add(New OleDb.OleDbParameter("Item", CType(txtboxItem.Text, String)))
            xcmd.Parameters.Add(New OleDb.OleDbParameter("Brand", CType(txtboxBrand.Text, String)))
            xcmd.Parameters.Add(New OleDb.OleDbParameter("Quantity", CType(txtboxQuantity.Text, String)))
            xcmd.Parameters.Add(New OleDb.OleDbParameter("Transaction_Type", CType(txtboxTransactionType.Text, String)))
            xcmd.Parameters.Add(New OleDb.OleDbParameter("Transaction_Date", CType(dtpTransactionDate.Text, String)))

            xcmd.ExecuteNonQuery()
            'You don't need to dispose now because End Using will take care of that
            'xcmd.Dispose()
        End Using

        xmyConnection.Close()
    End Using

    MsgBox("Process Completed Sucessfully")

'Exception catches everthing. You can catch others too so you can get more specific.
'When stacking them, you must go from most specific to least specific. So Exception will always be last
'I'm only adding these exceptions as examples. You can remove them.
'They are valid OleDbCommand exceptions
Catch exInvalidCast As InvalidCastException
    'Handle this
    MsgBox("Error: " & exInvalidCast.Message, MsgBoxStyle.Critical)
Catch exSQLException As SqlException
    'Handle this
    MsgBox("Error: " & exSQLException.Message, MsgBoxStyle.Critical)
Catch exIOException As IOException
    'Handle this
    MsgBox("Error: " & exIOException.Message, MsgBoxStyle.Critical)
Catch ex As Exception
    MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
Finally
    'always do this, even if error
    txtboxTransactionType.Clear()
End Try