我在该列中有一个表Metal_Master,其中包含Opening_Weight,我正在通过获取先前的值并添加一些值,然后再次更新该值来更新该列的值。
我的代码是
ConnectionDB ReturnMWeight = new ConnectionDB("SELECT Opening_Weight FROM Metal_Master WHERE Metal_Name='GOLD';");
DataTable weighttd = ReturnMWeight.returntable();
GoldW = GoldW + Convert.ToDouble(weighttd.Rows[0][0].ToString());
ConnectionDB AddMWeight = new ConnectionDB("UPDATE Metal_Master SET Opening_Weight=" + GoldW + " WHERE Metal_Name='GOLD';");
AddMWeight.AddData();但是我想在单个查询中直接更新值请帮助..
发布于 2013-02-04 13:41:14
您可以不运行select语句直接执行UPDATE,
UPDATE Metal_Master
SET Opening_Weight = Opening_Weight + new_Value
WHERE Metal_Name='GOLD'为了获得更好的代码质量,
using语句进行适当的对象处理try-catch进行意外的正确处理要防止sql injection的查询
发布于 2013-02-04 13:41:47
您可以在set的右侧使用列名。
ConnectionDB AddMWeight = new
ConnectionDB("UPDATE Metal_Master SET Opening_Weight = Opening_Weight " + 10 + " WHERE Metal_Name='GOLD';");发布于 2013-02-04 13:41:56
试试这个:
ConnectionDB AddMWeight = new ConnectionDB("UPDATE Metal_Master SET Opening_Weight=(SELECT SUM(Opening_Weight) AS Opening_Weight FROM Metal_master WHERE Metal_Name = 'GOLD')" + GoldW + " WHERE Metal_Name='GOLD';");
AddMWeight.AddData();https://stackoverflow.com/questions/14680962
复制相似问题