c# - Update Database from Dataset -
my goal update qty
each sku. using sqldataadapter
accomplish this. program runs fine. no result happens.
question: why no result happening? database remains unchanged.
code below
public static void updateinventoryfromamz(datatable datatable) { int index = 0; string connstring = system.configuration.configurationmanager.connectionstrings["myconnectionstring"].tostring(); dataset amzinventorydataset = new dataset("amzinvdataset"); amzinventorydataset.tables.add(datatable); // each row - perform update // using (sqlconnection connection = new sqlconnection(connstring)) { sqldataadapter adapter = new sqldataadapter(); foreach (datarow row in amzinventorydataset.tables[index].rows) { string sku = datatable.rows[index]["seller-sku"].tostring(); string qty = datatable.rows[index]["quantity"].tostring(); // create updatecommand. sqlcommand command = new sqlcommand( "update inventory set qty = @qty" + "where sku = @sku", connection); // add parameters updatecommand. command.parameters.add("@qty", sqldbtype.int, qty.length, qty); command.parameters.add("@sku", sqldbtype.varchar, sku.length, sku); adapter.updatecommand = command; adapter.update(amzinventorydataset.tables[index]); index++; } } }
when concatenating 2 sql strings, better add space @ end of first string or @ beginning of second string. gordon linoff pointed out sql statement incorrect. , setting parameters , values need change depending on type of parameters.
try below code, have use sqlcommand
, executenonquery
method update each row data
using (sqlconnection connection = new sqlconnection(connstring)) using (sqlcommand cmd = new sqlcommand("update inventory set qty = @qty sku = @sku", connection)) { connection.open(); var paramqty= cmd.parameters.add("@qty", sqldbtype.int); var parasku = cmd.parameters.add("@sku", sqldbtype.varchar); foreach (datarow row in amzinventorydataset.tables[0].rows) { parasku.value = row["seller-sku"].tostring(); paramqty.value = int.parse(row["quantity"].tostring()); cmd.executenonquery(); } }
Comments
Post a Comment