此代碼無效。彈出錯誤消息說:使用Visual Basic 2010更新MS Access數據庫時出錯
語法錯誤(缺少運算符)在查詢exprssion
SubName = Gussing Game
。
這是我的代碼,請幫助。我需要此代碼來更新Microsoft Access數據庫。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim Str As String
Con.Open()
Str = "update vb set AssignDate="
Str += """" & txtADate.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set DueDate="
Str += """" & txtDDate.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Weight="
Str += """" & txtWeight.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Reference="
Str += """" & txtReference.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Comment="
Str += """" & txtComment.Text & """"
Str += " where SubName ="
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Statues="
Str += """" & txtStatues.Text & """"
Str += " where SubName ="
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Dst.Clear()
Dad = New OleDbDataAdapter("SELECT * FROM vb ORDER BY SubName", Con)
Dad.Fill(Dst, "assignment")
MessageBox.Show("Record Updated!", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
MsgBox(ex.Message & "," & ex.Source)
End Try
Con.Close()
End Sub
如果SubName是一個文本字段,那麼它的每個值應該在單引號內。 _where SubName ='value'_。當然,如果價值包含自己的報價,則需要複製報價。你有沒有聽說過[參數化查詢](http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i)? – Steve
參數化您的查詢!你正在開放SQL注入。 – codechurn