使用c#更新ms访问

有人可以请帮助我的代码有什么问题吗? 它是一个更新function,在我的调试过程中它正确执行但它没有更新我的数据库。 我已经找到了解决这个问题的答案,但它仍然没有用。 我也尝试创建一个新的数据库,希望它是问题但仍然无效。

private void update_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); OleDbConnection con = new OleDbConnection(); con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\sherilyn & justine\Documents\Visual Studio 2015\Projects\jollibee4\jollibee4\jollibee.accdb"; con.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; String id = dataGridView1.SelectedRows[0].Cells[0].Value + String.Empty; int id1 = Int32.Parse(id); try { if (database.selectedIndex == 0) { cmd.CommandText = "update Breakfast_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 1) { cmd.CommandText = "update Burger_Sandwhich_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 2) { cmd.CommandText = "update Chicken_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 3) { cmd.CommandText = "update Dessert set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 4) { cmd.CommandText = "update Kids_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 5) { cmd.CommandText = "update RiceMeals_NoodlesMeals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 6) { cmd.CommandText = "update Side_Items set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } else if (database.selectedIndex == 7) { cmd.CommandText = "update Value_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id"; } cmd.Parameters.AddWithValue("@id", id1); cmd.Parameters.AddWithValue("@meals", meal.Text); int mealPrice = Int32.Parse(price.Text); cmd.Parameters.AddWithValue("@price", mealPrice); cmd.Parameters.AddWithValue("@picture", savePhoto()); cmd.Parameters.AddWithValue("@description",description.Text); DialogResult dialogResult = MessageBox.Show("Are you sure you want to change the data?","Warning", MessageBoxButtons.YesNo,MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { cmd.ExecuteNonQuery(); con.Close(); } else if (dialogResult == DialogResult.No) { con.Close(); } OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); adapter.Fill(dt); dataGridView1.DataSource = dt; database_onItemSelected(sender, e);//to view dgv data for the selected index con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error); con.Close(); } } 

按占位符的预期以正确的顺序添加参数

 cmd.Parameters.AddWithValue("@meals", meal.Text); int mealPrice = Int32.Parse(price.Text); cmd.Parameters.AddWithValue("@price", mealPrice); cmd.Parameters.AddWithValue("@picture", savePhoto()); cmd.Parameters.AddWithValue("@description",description.Text); cmd.Parameters.AddWithValue("@id", id1); 

OleDb不会按名称解析参数值,而是通过参数集合中参数的位置来解析参数值。 根据您的订单,where子句中的id条件接收description参数中的值。

还要考虑使用Add而不是AddWithValue

请参阅: 我们可以停止使用AddWithValue了吗?