title: How to use List<int> as SQL parameter in C#
body: <p>I am trying to use <code>List&lt; int&gt;</code> as SQL parameter using this code:</p>
  
  <pre><code>var listID= new List&lt;int&gt;() { 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 from MyTable where TableID in ( @tableID)";
  
                  string param = String.Join(",", listID.ToArray());
                  cmd.Parameters.Add("@tableID", param);
                  sqlConnection.Open();
                  cmd.ExecuteNonQuery();
              }
              sqlConnection.Close();
          }
  </code></pre>
  
  <p>The problem is, that this code will generate:</p>
  
  <blockquote>
    <p>exec sp_executesql N'delete from MyTable where TableID in (
    @tableID)',N'@tableID nvarchar(17)',@tableID =N'1,2,3,4,5,6,7,8,9'</p>
  </blockquote>
  
  <p>This will fail because: </p>
  
  <blockquote>
    <p>Conversion failed when converting the nvarchar value
    '1,2,3,4,5,6,7,8,9' to data type int.</p>
  </blockquote>
  
  <p>Any idea how to solve this? Thank you.</p>
  
  <p>EDIT: I'm using MS SQL 2012</p>