1

我试图在 ms 访问时使用 update 命令更新我的数据库,并且每当我尝试执行它时,都会为一个或多个必需参数提供 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);
        }
    }
4

1 回答 1

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

于 2013-11-03T10:31:04.470 回答