c# - Can someone tell me why this SQL query not working -
i followed answer,
how can supply list<int> sql parameter?
please see these questions of mine understanding scenario,
how can update crate ids of list of fruits in single sql query in c#
how can update sql table logic
what trying , not working
private void relate_fruit_crate(list<string> selectedfruitids, int selectedcrateid) { string updatestatement = "update relate_fruit_crate set crateid = @selectedcrateid fruitid = @selectedfruitids"; using (sqlconnection connection = new sqlconnection(connectionstring())) using (sqlcommand cmd = new sqlcommand(updatestatement, connection)) { connection.open(); cmd.parameters.add(new sqlparameter("@selectedcrateid", selectedcrateid.tostring())); cmd.parameters.add(new sqlparameter("@selectedfruitids", string.join(",",selectedfruitids.toarray()))); cmd.executenonquery(); } } my code runs without error,
you need use in keyword in scenario. problem sqlcommand.parameters pattern not build query itself, calls stored procedure on database:
exec sp_executesql n'update relate_fruit_crate set crateid = @selectedcrateid fruitid in(''@selectedfruitids'')', n'@selectedcrateid nvarchar(1),@selectedfruitids nvarchar(5)', @selectedcrateid = n'1', @selectedfruitids = n'1,2' this not work array escaped.
the workaround either use normal stringbuilder create query. (warning! sql injection) or call query each id separately.
maybe there's way sqlcommand.parameters, not find one.
old post::
string updatestatement = "update relate_fruit_crate set crateid in ('@selectedcrateid') fruitid = '@selectedfruitids'"; [....]
cmd.parameters.add(new sqlparameter("@selectedfruitids", string.join("','",selectedfruitids.toarray()))); and equals (=) query match single value.
Comments
Post a Comment