我试图使用update命令在ms访问时更新我的数据库 是每当我尝试执行它时,给出一个或多个必需参数的No Value错误。
这是我的代码
private void btnupdate_Click_1(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ShopRecords.accdb");
OleDbDataAdapter ad = new OleDbDataAdapter();
try
{
ad.UpdateCommand = new OleDbCommand("UPDATE ShopRecords SET ProductDescription = '" +tbproductdescrip.Text + "' WHERE (ID= " + tbupdate.Text + ")", con);
con.Open();
ad.UpdateCommand.ExecuteNonQuery();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:1)
尝试遵循下一个结构:
try
{
using (OleDbConnection con = new OleDbConnection(cs))
{
con.Open();
OleDbTransaction tran = con.BeginTransaction();
OleDbCommand cmd = new OleDbCommand("UPDATE ... SET ... WHERE ...", con);
cmd.Transaction = tran;
cmd.ExecuteNonQuery();
tran.Commit();
con.Close();
}
}
catch (OleDbException ex)
{
Console.WriteLine(ex);
}
也是一个很好的例子:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtransaction.commit%28v=vs.110%29.aspx