.net - How to use List<int> as SQL parameter in C# -


this question has answer here:

i trying use list< int> sql parameter using code:

var listid= new list<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };          using (var sqlconnection = new sqlconnection(_connectionstring))         {             using (var cmd = new sqlcommand())             {                 cmd.connection = sqlconnection;                 cmd.commandtext = "delete mytable tableid in ( @tableid)";                  string param = string.join(",", listid.toarray());                 cmd.parameters.add("@tableid", param);                 sqlconnection.open();                 cmd.executenonquery();             }             sqlconnection.close();         } 

the problem is, code generate:

exec sp_executesql n'delete mytable tableid in ( @tableid)',n'@tableid nvarchar(17)',@tableid =n'1,2,3,4,5,6,7,8,9'

this fail because:

conversion failed when converting nvarchar value '1,2,3,4,5,6,7,8,9' data type int.

any idea how solve this? thank you.

edit: i'm using ms sql 2012

you should use tvp, can keep query specified. introduced in sql 2008.

table valued parameter, example


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -