简体   繁体   中英

MS Access Update Query Not Working

I can't Figure out what is wrong with the query, but it is not updating any value in the Table

string qry = "UPDATE Stock SET  Itemname=@n,Unit=@u,Price=@p,Tax=@t,Balance=@b,Status=@s Where Sid=@sid";
OleDbCommand ocmd = new OleDbCommand(qry,BBC);
ocmd.Parameters.AddWithValue("@n", name);
ocmd.Parameters.AddWithValue("@u", unit);
ocmd.Parameters.AddWithValue("@p", price);
ocmd.Parameters.AddWithValue("@t", tax);
ocmd.Parameters.AddWithValue("@b", balance);
ocmd.Parameters.AddWithValue("@s", status);
ocmd.Parameters.AddWithValue("@sid", sid);
ocmd.ExecuteNonQuery();

Price , Tax and Balance are Decimal values.

I did debug and its working fine but just not updating the value.

set the type of each parameter and execute the query.

List<OleDbParameter> paramList = new List<OleDbParameter>();

OleDbParameter param = new OleDbParameter("@param", OleDbType.TypeName);

param.Value = value;

paramList.Add(param);

ocmd.Parameters.AddRange(paramArray);

I think this will do it for you.

string ConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\your_path_here\test.accdb";
using(OleDbConnection conn = new OleDbConnection(ConnString ))
using(OleDbCommand cmd = conn.CreateCommand())
{
   cmd.CommandText = @ "UPDATE Stock SET  Itemname=@n,Unit=@u,Price=@p,Tax=@t,Balance=@b,Status=@s Where Sid=@sid";

   cmd.Parameters.AddWithValue("@n", txtItemName.Text);
   cmd.Parameters.AddWithValue("@u", txtUnit.Text);
   cmd.Parameters.AddWithValue("@p", Convert.ToDecimal(txtPrice.Text));
   cmd.Parameters.AddWithValue("@t", Convert.ToDecimal(txtTax.Text));
   cmd.Parameters.AddWithValue("@b", Convert.ToDecimal(txtBalance.Text));
   cmd.Parameters.AddWithValue("@s", txtStatus.Text);
   cmd.Parameters.AddWithValue("@sid", txtSID.Text);

   conn.Open();
   int rowsAffected = cmd.ExecuteNonQuery();
   conn.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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