验证数据库查询是否成功

时间:2014-02-25 11:53:02

标签: sql-server vb.net database-connection sqlconnection

我有这个测试函数将一些示例数据插入表中。我想要的是关于手术成功的反馈。这是功能:

Public Function insertTest(ByVal userEmail As String) As Boolean

        Dim result As Boolean = False

        Try

            Dim connection As New SqlConnection(connectionString)
            connection.Open()
            Dim sqlQuery As String = "insert into _TEST_activityLog values (@userEmail, 'delete', 'folder X')"
            Dim command As New SqlClient.SqlCommand(sqlQuery, connection)
            command.Parameters.Add("@userEmail", SqlDbType.VarChar).Value = userEmail

            Dim rdr = command.ExecuteReader()
            If rdr.HasRows Then

                MsgBox("Record created successfully")
                command.Dispose()

            End If

            rdr.Close()
            command.Dispose()
            connection.Close()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

        Return result

    End Function

我尝试使用reader部分,但它总是跳过它。顺便说一句,数据已成功插入数据库。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您必须调用ExecuteNonQuery方法进行插入操作,而不是ExecuteReader。如果有错误,它将抛出异常。方法返回受影响的行数。