What is the maximum length of a SqlCommand query?
<p>I am currently updating and reading values with a C# script from SQL Server 2014. When using a SQlCommand to executeNonQuery, it pops out an error when running the script:</p>

<blockquote>
  <p>IndexOutOfRangeException: Array index is out of range.<br>
  Mono.Data.Tds.Protocol.TdsComm.AppendInternal (Int16 s)<br>
  Mono.Data.Tds.Protocol.TdsComm.Append (System.String s)<br>
  Mono.Data.Tds.Protocol.Tds70.WriteRpcParameterInfo (Mono.Data.Tds.TdsMetaParameterCollection parameters)<br>
  Mono.Data.Tds.Protocol.Tds70.ExecRPC (TdsRpcProcId rpcId, System.String sql, Mono.Data.Tds.TdsMetaParameterCollection parameters, Int32 timeout, Boolean wantResults)<br>
  Mono.Data.Tds.Protocol.Tds70.Execute (System.String commandText, Mono.Data.Tds.TdsMetaParameterCollection parameters, Int32 timeout, Boolean wantResults)<br>
  System.Data.SqlClient.SqlCommand.Execute (Boolean wantResults)<br>
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery ()<br>
  (wrapper remoting-invoke-with-check)<br>
  System.Data.SqlClient.SqlCommand:ExecuteNonQuery ()<br>
  Database.DataBaseConnection.Update () (at Assets/DataBaseConnection.cs:674)</p>
</blockquote>

<p>I counted how many characters the <code>SqlCommand</code> has, and it is 8,125 characters (no spaces), 10,874 characters (with spaces).</p>

<p>There are 198 parameters but I guess that it is not due to that, because somewhere I read that the maximum amount of parameters for a single query was 2000, am I right?</p>

<p>I reduced the number of parameters (till 20 parameters) and consequently, command length and it works like a charm (875 characters without spaces and 1,221 characters with spaces).</p>

<p>To sum up, my question is: what is the maximum length of a <code>SqlCommand</code> query in SQL Server 2014? And in SQL Server 2008?</p>

<p><strong>Sample of my code:</strong></p>

<pre><code>//New command to update values in input table in sql server
using (SqlCommand command = new SqlCommand("UPDATE DigitalInputs" +
    " SET Value = CASE Name" +
    " WHEN @LI_Input_Variable1_Name THEN @LI_Input_Variable1_Value" +
    " WHEN @LI_Input_Variable2_Name THEN @LI_Input_Variable2_Value" +
    " WHEN @LI_Input_Variable3_Name THEN @LI_Input_Variable3_Value" +
    //It is the same from 3 till 99
    " WHEN @LI_Input_Variable99_Name THEN @LI_Input_Variable99_Value" +
    " END" +
    " WHERE Name IN (@LI_Input_Variable1_Name, @LI_Input_Variable2_Name, @LI_Input_Variable3_Name,
    //It is the same from 3 till 99
    @LI_Input_Variable99_Name);", connection))
{

command.Parameters.Add(new SqlParameter("LI_Input_Variable1_Name", "LI_Input_Variable1"));
command.Parameters.Add(new SqlParameter("LI_Input_Variable1_Value", LI_Input_Variable1.ToString()));
command.Parameters.Add(new SqlParameter("LI_Input_Variable2_Name", "LI_Input_Variable2"));
command.Parameters.Add(new SqlParameter("LI_Input_Variable2_Value", LI_Input_Variable2.ToString()));
command.Parameters.Add(new SqlParameter("LI_Input_Variable3_Name", "LI_Input_Variable3"));
command.Parameters.Add(new SqlParameter("LI_Input_Variable3_Value", LI_Input_Variable3.ToString()));
//It is the same from 3 till 99
command.Parameters.Add(new SqlParameter("LI_Input_Variable99_Name", "LI_Input_Variable99"));
command.Parameters.Add(new SqlParameter("LI_Input_Variable99_Value", LI_Input_Variable99.ToString()));

command.ExecuteNonQuery(); //Execute the non query
}
</code></pre>

<p><strong>Post-Edited:</strong>
I'm implementing this script with MonoDevelop 5.9.6. in Unity3D</p>