2014-01-28 88 views
0

只是想問一下在ms訪問數據庫中更新數據的正確方法是什麼,因爲當我使用此代碼時,我的數據沒有更新並且沒有顯示任何錯誤,所以函數的返回值是FALSE。下面是 是我的代碼。如何更新MS Access數據庫中的數據

dim conn as new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\myDb.accdb;Persist Security Info=False;") 
Protected Function UpdateProduct(ByVal productDetails As ProductModel) As Boolean 
    reopenConnection() 
    cmd = New OleDbCommand("UPDATE Product Set [email protected], [email protected], [email protected], [email protected] where [email protected];", conn) 
    cmd.CommandType = CommandType.Text 
    cmd.Parameters.Add("@prodId", OleDbType.VarChar).Value = productDetails.NewProductId 
    cmd.Parameters.Add("@prodName", OleDbType.VarChar).Value = productDetails.ProductName 
    cmd.Parameters.Add("@price", OleDbType.Decimal).Value = productDetails.Price 
    cmd.Parameters.Add("@prodDesc", OleDbType.VarChar).Value = productDetails.ProductDescription 
    cmd.Parameters.Add("@categoryId", OleDbType.Integer).Value = productDetails.CategoryId 
    Return cmd.ExecuteNonQuery() > 0 
End Function 

Private Sub reopenConnection() 
    If conn.State = ConnectionState.Open Then 
     conn.Close() 
    End If 
    conn.Open() 
End Sub 

感謝

+1

您是否嘗試過把你的參數完全相同的順序,因爲它們發生在你的理由?參數的名稱不被使用,只有位置。 – Fionnuala

回答

0

我訪問的經驗是,你必須提交事務或你永遠看不到在數據庫中更新。查看此語法的stackoverflow線程。

How to implement transaction way in vb.net?

+0

嗨,提請感謝提示,我不認爲它總是需要實現一個事務來操縱數據,下面是我的其他更新代碼,它工作正常。 – user2928162

+0

這是我的代碼 保護的函數UpdateCategory(categoryDe​​tails作爲CategoryModel)爲布爾 reopenConnection() CMD =新的OleDbCommand( 「UPDATE [類別]中設定類別名稱= @categoryName其中類別ID = @CategoryID」,康涅狄格州) CMD。的CommandType = CommandType.Text cmd.Parameters.Add( 「@類別名稱」,OleDbType.VarChar)。價值= categoryDe​​tails.NewCategoryName cmd.Parameters.Add( 「@的categoryId」,OleDbType.Integer)。價值= categoryDe​​tails。 CategoryId Return cmd.ExecuteNonQuery()> 0 End Function – user2928162

+0

Then I會在updateProduct函數上設置一個斷點,進入它並檢查正在執行的SQL語法。它可以成功運行,但不會因其他原因而更新任何內容。 – 2014-01-28 07:46:13