ADO.NET parameters for use with the SQL “IN” Keyword

Long of the short of it is there isn’t a good way to do it.

A common example is that you might want select multiple rows from a table, using a list of IDs:

SELECT * FROM room WHERE room_id IN (1,2,3,4);

What you’d like to do is have this list as an array parameter and set it appropriately on the command:


int ids = new int[] { 1, 2, 3, 4 };
string sql = "SELECT * FROM room WHERE room_id IN @IDS;";

SqlCommand cmd = new SqlCommand(sql, conn);

// DOES NOT WORK!
cmd.Parameters.Add("@IDS", ids);

The problem is there is no array type in SQL server/ADO.NET. The only way to accomplish this is through dynamic sql, which is definitely not a best practice.

There are several articles discussing this problem. The following are taken from this forum discussion in which Arnie Rowland provided the links.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>