c# - How to update multiple rows in a table without using a loop? -
i'm there must way this, executenonquery
wouldn't exist if couldn't alter more 1 row @ time, can't figure out how. @ moment i'm updating values in customer table code:
(int = 0; < datagridview1.rowcount; i++) { string connectionstring = @"data source = a103-17\sqlexpress17; initial catalog = carrental; integrated security = true"; string myupdate = "update [carrental].[dbo].[customer] " + // "set [customerid] = @customerid, " + "set [firstname] = @firstname, " + "[lastname] = @lastname, " + "[streetno] = @streetno, " + "[streetname]= @streetname, " + "[suburb] = @suburb, " + "[state] = @state, " + "[postcode] = @postcode, " + "[mobphone] = @mobphone, " + "[driverlicno] = @driverlicno, " + "[creditcardtype] = @creditcardtype, " + "[creditcardno] = @creditcardno, " + "[expdate] = @expdate, " + "[nameoncreditcard] = @nameoncreditcard " + "where [customerid] = @customerid" + ";"; sqlconnection con = new sqlconnection(connectionstring); using (sqlcommand apptupdate = new sqlcommand(myupdate, con)) { apptupdate.parameters.addwithvalue("@customerid", datagridview1.rows[i].cells["customerid"].value ?? dbnull.value); apptupdate.parameters.addwithvalue("@firstname", datagridview1.rows[i].cells["firstname"].value ?? "null"); apptupdate.parameters.addwithvalue("@lastname", datagridview1.rows[i].cells["lastname"].value ?? "null"); apptupdate.parameters.addwithvalue("@streetno", datagridview1.rows[i].cells["streetno"].value ?? "null"); apptupdate.parameters.addwithvalue("@streetname", datagridview1.rows[i].cells["streetname"].value ?? "null"); apptupdate.parameters.addwithvalue("@suburb", datagridview1.rows[i].cells["suburb"].value ?? "null"); apptupdate.parameters.addwithvalue("@state", datagridview1.rows[i].cells["state"].value ?? "null"); apptupdate.parameters.addwithvalue("@postcode", datagridview1.rows[i].cells["postcode"].value ?? "null"); apptupdate.parameters.addwithvalue("@mobphone", datagridview1.rows[i].cells["mobphone"].value ?? "null"); apptupdate.parameters.addwithvalue("@driverlicno", datagridview1.rows[i].cells["driverlicno"].value ?? "null"); apptupdate.parameters.addwithvalue("@creditcardtype", datagridview1.rows[i].cells["creditcardtype"].value ?? "null"); apptupdate.parameters.addwithvalue("@creditcardno", datagridview1.rows[i].cells["creditcardno"].value ?? "null"); apptupdate.parameters.addwithvalue("@expdate", datagridview1.rows[i].cells["expdate"].value ?? datetime.now); apptupdate.parameters.addwithvalue("@nameoncreditcard", datagridview1.rows[i].cells["nameoncreditcard"].value ?? "null"); con.open(); messagebox.show(apptupdate.executenonquery().tostring()); con.close(); }; } }
this works, messagebox pop showing value of 1 each row, rather once showing value corresponding how many rows changed. can't move execution outside of loop, can't figure out how use out of rows affected return of executenonquery
. how can use feature correctly?
combine customerid's comma separated string first. instead of where customerid = @customerid
use where customerid in (@customerids)
@customerids
string
Comments
Post a Comment