检查是否插入行

| 我正在尝试检查是否将一行插入到数据库中。即使插入,它也会引发错误(这是一个数据库问题,当我获得较新版本的Microsoft Access时,该问题将在以后得到解决),因此我无法根据是否存在插入来检查插入是否成功\是不是错误。我想我需要检查某些东西的AffectedRows,但是我不确定。我一直在寻找有关如何执行此操作的信息,但我无法弄清楚如何使其适合我的实际情况。这是我的代码的大致概念:
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick

\'code for collecting data...

\'Define Connection
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = AccessDataSource1.ConnectionString
    myConn.Open()

    \'Insert command
    Dim myIns1 As New OleDbCommand(\"INSERT INTO tableCourse \'long insert command here...

\'Execute command and handle errors
    Try
        myIns1.ExecuteNonQuery()
    Catch myException5 As Exception
    End Try

    \'Close connection
    myConn.Close()
-更新- 我这样尝试过:
\'Execute command, handle errors, and check if row was inserted
    Dim numInserted As Integer = 0
    Try
        numInserted = myIns1.ExecuteNonQuery()
    Catch myException As Exception
    Finally
        If numInserted = 0 Then
            Label1.Text = \"Sorry, an error occured.\"
        Else
            Label1.Text = \"Thank you! Your new course approval request has been submitted.\"
        End If
    End Try
但是,即使插入成功,numInserted每次也会显示为0。即使插入成功,myIns1.ExecuteNonQuery()也会引发错误。 -EDIT-我发现\“重复值\”错误是因为它以某种方式试图将记录插入两次。我不知道为什么要这么做。
已邀请:
ExecuteNonQuery函数返回受影响的行数...
Dim numInserted as Integer = myIns1.ExecuteNonQuery()
If numInserted = 0 Then
   \' no rows were inserted...throw exception?
End If
您所描述的症状(未知重复,但插入会引发重复错误)听起来像Access数据库需要压缩和修复。 您应该定期执行此操作。 (始终先备份)。

要回复问题请先登录注册