sql server - Sql "IN" operator with using SqlParameter on varchar field -
i couldn't find how use in
operator sqlparameter
on varchar
column. please check out @mailbox
parameter below:
using (sqlcommand command = new sqlcommand()) { string sql = @"select ei.id interactionid, eo.sentdate mailreplieddate bla bla mailbox in (@mailbox)"; command.commandtext = sql; command.connection = conn; command.commandtype = commandtype.text; command.parameters.add(new sqlparameter("@mailbox", mailbox)); sqldatareader reader = command.executereader(); }
i tried these strings , query doesn't work.
string mailbox = "'abc@abc.com','def@def.com'" string mailbox = "abc@abc.com,def@def.com"
i have tried changed query mailbox in('@mailbox')
, string mailbox = "abc@abc.com,def@def.com"
any suggestions? thanks
that doesn't work way.
you can parameterize each value in list in in
clause:
string sql = @"select ei.id interactionid, eo.sentdate mailreplieddate bla bla mailbox in ({0})"; string mailbox = "abc@abc.com,def@def.com"; string[] mails = mailbox.split(','); string[] paramnames = mails.select((s, i) => "@tag" + i.tostring()).toarray(); string inclause = string.join(",", paramnames); using (var conn = new sqlconnection("connectionstring")) using (sqlcommand command = new sqlcommand(sql, conn)) { (int = 0; < paramnames.length; i++) { command.parameters.addwithvalue(paramnames[i], mails[i]); } conn.open(); using (sqldatareader reader = command.executereader()) { // ... } }
adapted from: https://stackoverflow.com/a/337792/284240
Comments
Post a Comment