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:
IndexOutOfRangeException: Array index is out of range.
Mono.Data.Tds.Protocol.TdsComm.AppendInternal (Int16 s)
Mono.Data.Tds.Protocol.TdsComm.Append (System.String s)
Mono.Data.Tds.Protocol.Tds70.WriteRpcParameterInfo (Mono.Data.Tds.TdsMetaParameterCollection parameters)
Mono.Data.Tds.Protocol.Tds70.ExecRPC (TdsRpcProcId rpcId, System.String sql, Mono.Data.Tds.TdsMetaParameterCollection parameters, Int32 timeout, Boolean wantResults)
Mono.Data.Tds.Protocol.Tds70.Execute (System.String commandText, Mono.Data.Tds.TdsMetaParameterCollection parameters, Int32 timeout, Boolean wantResults)
System.Data.SqlClient.SqlCommand.Execute (Boolean wantResults)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery ()
(wrapper remoting-invoke-with-check)
System.Data.SqlClient.SqlCommand:ExecuteNonQuery ()
Database.DataBaseConnection.Update () (at Assets/DataBaseConnection.cs:674)
I counted how many characters the SqlCommand
has, and it is 8,125 characters (no spaces), 10,874 characters (with spaces).
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?
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).
To sum up, my question is: what is the maximum length of a SqlCommand
query in SQL Server 2014? And in SQL Server 2008?
Sample of my 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
}
Post-Edited: I'm implementing this script with MonoDevelop 5.9.6. in Unity3D
Let me answer the question you ask: What is the maximum length of a SqlCommand query?.
The max size of a statement is 65536*Network Packet Size - which by default is around 1500 bytes. Make the math - that is around 90MB.
Exceeding this, though will NOT lead to your error. But it is the question you did ask.