Unable to update SQL Database, Using C# Win Form -
my application (c# win form) working while ago - i.e (update, insert, delete...). after close program , open database there no changes being made.
i'm not getting errors during running. i'm using vs2013 professional, sql database, c#.
using (sqlconnection connection = new sqlconnection(constring)) { sqlcommand cmd = new sqlcommand("update [fullink] set [instock] = '" + newsum + "' [catalog] = '" + catalog + "'"); cmd.commandtype = commandtype.text; cmd.connection = connection; connection.open(); cmd.executenonquery(); connection.close(); }
in absence of additional transaction such transactionscope
or sqltransaction
(which can't see in code), there no reason why update rolled back. believe might not updating data think.
although not solution, better practice use parameterized queries rather using strings - has security (sql injection), performance (query plan caching) , helps eliminate bugs relating quotes, escaping, , type conversion (which might case here - e.g. inserting newsum
imply numeric value instock
using quotes, implies char type). e.g.
using (var connection = new sqlconnection(constring)) using (var cmd = new sqlcommand("update [fullink] set [instock] = @instock [catalog] = @catalog")) { cmd.commandtype = commandtype.text; cmd.connection = connection; cmd.parameters.addwithvalue("@instock", newsum); cmd.parameters.addwithvalue("@catalog", catalog); connection.open(); cmd.executenonquery(); }
other minor modifications include disposing of sqlcommand
, , note disposing connection close it, won't need explicitly close (although doing won't hurt).
Comments
Post a Comment