2013-05-19 74 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