使用C#更新MS Access [英] update in ms access using c#

查看:67
本文介绍了使用C#更新MS Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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不会通过名称来解析参数值,而是通过参数在参数集合中的位置来解析.使用您的订单,where子句中的 id 条件从description参数接收值.

OleDb doesn't resolve parameters values by their name, but by the parameter's position in the parameters collection. With your order the id condition in the where clause receives the value from the description parameter.

还考虑使用Add代替AddWithValue

Consider also to use Add instead of AddWithValue

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

这篇关于使用C#更新MS Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆