【问题标题】:update in ms access using c#使用 c# 在 ms 访问中更新
【发布时间】:2018-10-24 06:20:09
【问题描述】:

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

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();
}
}

【问题讨论】:

  • 呃。所以不是最小的可行例子..好的..所以。当你跟踪它时会发生什么,它是否运行更新命令..
  • 当我尝试放置断点时,它会转到ExecuteNonQuery(),所以我认为这意味着我的更新成功但是在查看我的数据库后它仍然没有更新

标签: c# database ms-access insert-update


【解决方案1】:

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

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

见:Can we stop using AddWithValue already?

【讨论】:

  • 我没想到这是我的问题的原因。但它现在有效。谢谢你:)
  • 我会举手的,没想到顺序很重要。
猜你喜欢
  • 2017-09-10
  • 1970-01-01
  • 2016-06-02
  • 2020-10-12
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多