2013-05-19 73 views
0

嗨我用vb.net更新我的一個表時遇到問題,當我點擊按鈕更新數據庫時,它給我錯誤「System.Data .OleDb.OleDbException:沒有給出一個或多個必需參數的值。「 這裏是代碼如何更新vb.net中的MS Access數據庫

Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click 
    Dim aConnection As New OleDbConnection 
    Dim aCommand As New OleDbCommand 
    Dim SQLQuery, aConnectionString As String 
    Dim Text As String = TextBox1.Text 
    Dim aDataAdapter As New OleDbDataAdapter 
    Dim aDataReader As New DataSet 
    SQLQuery = "Update Review Set report='Yes' Where Text='" & Text & "'" 
    aConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath("App_Data/BookReviewWebsite.accdb") 
    aConnection = New OleDbConnection(aConnectionString) 
    aConnection.Open() 

    aCommand = New OleDbCommand(SQLQuery, aConnection) 

    aCommand.ExecuteNonQuery() 


    Label15.Text = "Review has been reported" 

    aConnection.Close() 

End Sub 

回答

0

Review, Report and Text。在這裏你有兩個字段和一個表名,你應該檢查你的數據庫是否有效地包含一個名爲Review的表,如果在這個表中有兩個字段叫做reportText

如果你真的有這個表,並且這些字段,那麼你就需要用方括號括詞TEXT因爲字文是在Access中保留關鍵字2007

最後,但並非最不重要的你的問題是查詢字符串本身。以這種方式連接字符串可能是錯誤的來源。如果你在你的「文本」單引號變量,則結果可能是impredictable,範圍從語法錯誤Sql Injection

SQLQuery = "Update Review Set report='Yes' Where [Text]=?" 
aCommand = New OleDbCommand(SQLQuery, aConnection) 
aCommand.Parameter.AddWithValue("@p1", Text) 
aCommand.ExecuteNonQuery() 
+0

謝謝SOOO很多這真是救了我的屁股 – jonathan