我目前正在使用C#脚本从SQL Server 2014更新和读取值。当使用SqlCommand
执行非查询操作时,运行脚本时会出现错误:
IndexOutOfRangeException: 数组索引超出范围。
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)
我数了一下SqlCommand
有多少个字符,它有8,125个字符(没有空格),10,874个字符(带空格)。
虽然有198个参数,但我想这不是由于参数数量导致的问题,因为我在某个地方读到单个查询的最大参数量为2000,我是正确的吗?
我减少了参数数量(至20个参数),从而减小了命令长度,并且它运行得很好(没有空格的875个字符和带有空格的1,221个字符)。
总之,我的问题是:在SQL Server 2014中SqlCommand
查询的最大长度是多少?在SQL Server 2008中呢?
我的代码示例:
//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
}
我正在使用MonoDevelop 5.9.6在Unity3D中实现此脚本。
100 x 3 x LEN(@LI_Input_Variable1_abcde)
是非常多的,100 x LEN(@vXX) + 200 x LEN(@nXX)
则少得多。 - Shnugo