2015-11-01 98 views
0

此代碼無效。彈出錯誤消息說:使用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 
+0

如果SubName是一個文本字段,那麼它的每個值應該在單引號內。 _where SubName ='value'_。當然,如果價值包含自己的報價,則需要複製報價。你有沒有聽說過[參數化查詢](http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i)? – Steve

+2

參數化您的查詢!你正在開放SQL注入。 – codechurn

回答

1

你沒給你報以比較SubName串標:

Str = "update vb set AssignDate=" 
    Str += """" & txtADate.Text & """" 
    Str += "where SubName = " 
    Str += txtAName.Text.Trim() ' should be """"&txtAName.Text.Trim()&"""" 

我會跳過此查詢的一種極其惡劣的方式是什麼。

+0

但它沒有更新。顯示了相同的錯誤消息。 –

+0

你是否解決了其他問題?每個查詢重複相同的部分。 – Blindy

+0

另外,日期被''包圍,而不是引號。 – Crowcoder

相關問題