使用VB Update不会更新我的数据库

时间:2015-04-23 18:03:02

标签: vb.net

我的插入代码有效,但我的更新无效"正确"。当我的标题文本框条目与标题数据库列中的内容匹配时,它应该更新该条目,它将跟进并弹出消息框"条目更新"但它没有更新。

这是我的代码:

Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
    Dim sql As String

    Dim con As New OleDb.OleDbConnection

    Dim cmd As New OleDb.OleDbCommand

    con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Tyler\Desktop\TylerPhillipsIP1.mdb"

    con.Open()

    Try
        sql = "INSERT INTO Books([Title], [Author], [Artist], [Chapter Number], [Page Number], [Genre])  VALUES('" & Me.TxtTitle.Text & "','" & Me.TxtAuthor.Text & "','" & Me.TxtArtist.Text & "','" & Me.TxtChapterNumber.Text & "','" & Me.TxtPageNumber.Text & "','" & Me.TxtGenre.Text & "')"
        cmd = New OleDb.OleDbCommand(sql, con)
        cmd.ExecuteNonQuery()
        MsgBox("Entry Saved")
    Catch ex As Exception
        sql = "UPDATE Books SET [Author]=@Author, [Artist]=@Artist, [Chapter Number]=@Chapter_Number, [Page Number]=@Page_Number, [Genre]=@Genre WHERE [Title]=@Title;"
        cmd = New OleDb.OleDbCommand(sql, con)
        cmd.Parameters.AddWithValue("@Title", TxtTitle.Text)
        cmd.Parameters.AddWithValue("@Author", TxtAuthor.Text)
        cmd.Parameters.AddWithValue("@Artist", TxtArtist.Text)
        cmd.Parameters.AddWithValue("@Chapter_Number", (TxtChapterNumber.Text).ToString())
        cmd.Parameters.AddWithValue("@Page_Number", (TxtPageNumber.Text).ToString())
        cmd.Parameters.AddWithValue("@Genre", TxtGenre.Text)
        cmd.ExecuteNonQuery()
        MsgBox("Entry Updated")
    Finally
        TxtTitle.Text = ""
        TxtAuthor.Text = ""
        TxtArtist.Text = ""
        TxtChapterNumber.Text = ""
        TxtPageNumber.Text = ""
        TxtGenre.Text = ""

        con.Close()
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

为了本练习,我不会对代码进行优化/参数化。我将展示如何知道更新是否真的有效。我担心你的where条件有问题。让我们稍微重构你的代码 - 并在代码中阅读注释!

Dim cmd As New OleDb.OleDbCommand

con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Tyler\Desktop\TylerPhillipsIP1.mdb"

con.Open()

sql = "SELECT count(*) FROM Books WHERE [Title]=@Title;"
cmd = New OleDb.OleDbCommand(sql, con)
dim result as integer = Convert.ToInt32(cmd.ExecuteScalar())

If result = 0 Then  

    sql = "INSERT INTO Books([Title], [Author], [Artist], [Chapter Number], [Page Number], [Genre])  VALUES('" & Me.TxtTitle.Text & "','" & Me.TxtAuthor.Text & "','" & Me.TxtArtist.Text & "','" & Me.TxtChapterNumber.Text & "','" & Me.TxtPageNumber.Text & "','" & Me.TxtGenre.Text & "')"
    cmd = New OleDb.OleDbCommand(sql, con)
    ' If the row inserted, the result will be > 0
    result = cmd.ExecuteNonQuery()
    If result > 0 Then 
        MsgBox("Insert Success")
    Else
        MsgBox("Insert Failure")
    End if

Else

    sql = "UPDATE Books SET [Author]=@Author, [Artist]=@Artist, [Chapter Number]=@Chapter_Number, [Page Number]=@Page_Number, [Genre]=@Genre WHERE [Title]=@Title;"
    cmd = New OleDb.OleDbCommand(sql, con)
    cmd.Parameters.AddWithValue("@Title", TxtTitle.Text)
    cmd.Parameters.AddWithValue("@Author", TxtAuthor.Text)
    cmd.Parameters.AddWithValue("@Artist", TxtArtist.Text)
    cmd.Parameters.AddWithValue("@Chapter_Number", (TxtChapterNumber.Text).ToString())
    cmd.Parameters.AddWithValue("@Page_Number", (TxtPageNumber.Text).ToString())
    cmd.Parameters.AddWithValue("@Genre", TxtGenre.Text)
    result = cmd.ExecuteNonQuery()

    ' For the UPDATE, this is the key - to check if anything was updated. Because UPDATE is naturally will not throw any errors and you will not ever know, unless you send bad data or try to duplicate a unique key.
    If result > 0 Then 
        MsgBox("Update Success")
    Else
        MsgBox("Update Failure")
    End if


    con.Close()

End If

出于本练习的目的,我省略了如何处理错误,参数化和控制数据流。我想表明,如果您在更新后使用result并且其值为>0 - 您确实更新了该行。只有在一些叛国罪中你才不知道。如果结果为0,则表示您未进行更新。然后,您需要查看您所在的位置:WHERE [Title]=@Title。可能是您更新不存在的标题。除非您在进行数据库更新时检查ExecuteNonQuery的返回,否则您无法确定是否更新了。我希望这会对你有所帮助