简体   繁体   English

使用C#更新MS Access

[英]update in ms access using c#

can someone please help what is wrong with my code? 有人可以帮忙我的代码有什么问题吗? it is a update function and during my debug it executing properly but it is not updating my database. 它是一个更新功能,在我的调试过程中,它可以正常执行,但未更新数据库。 I already search for an answer to for this problem but still it didn't work. 我已经在寻找这个问题的答案,但是仍然没有用。 i also try to create a new database hoping that it is problem but still no effect. 我还尝试创建一个新数据库,希望它是有问题的,但仍然没有效果。

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

Add the parameters in the correct order as expected by the placeholders 按照占位符期望的正确顺序添加参数

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 doesn't resolve parameters values by their name, but by the parameter's position in the parameters collection. OleDb不会通过名称来解析参数值,而是通过参数在参数集合中的位置来解析。 With your order the id condition in the where clause receives the value from the description parameter. 使用您的订单,where子句中的id条件从description参数接收值。

Consider also to use Add instead of AddWithValue 还考虑使用Add代替AddWithValue

See: Can we stop using AddWithValue already? 请参阅: 我们可以停止使用AddWithValue吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM