【发布时间】:2016-04-01 09:52:49
【问题描述】:
我目前正在使用 SQL Server 2014 中的 C# 脚本更新和读取值。当使用 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 参数)
Mono.Data.Tds.Protocol.Tds70.ExecRPC (TdsRpcProcId rpcId, System.String sql, Mono.Data.Tds.TdsMetaParameterCollection 参数, Int32 超时, Boolean wantResults)
Mono.Data.Tds.Protocol.Tds70.Execute(System.String commandText,Mono.Data.Tds.TdsMetaParameterCollection 参数,Int32 超时,布尔值 wantResults)
System.Data.SqlClient.SqlCommand.Execute(布尔值 wantResults)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery ()
(包装器远程调用与检查)
System.Data.SqlClient.SqlCommand:ExecuteNonQuery ()
Database.DataBaseConnection.Update () (at Assets/DataBaseConnection.cs:674)
我数了一下SqlCommand有多少个字符,是8125个字符(没有空格),10874个字符(有空格)。
有 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 中
【问题讨论】:
-
我不认为这是
parameter length的问题。必须有一些parameter这是一种数组,它会导致IndexOutOfRange Exception..没有更多细节,很难回答到底是什么导致了这个问题.. -
如果你不得不问......它太长了:)
-
不要只给我们一个长代码转储 - 确保您创建一个minimal reproducible example。强调最小。
-
尝试使用较短的名称 :-)
100 x 3 x LEN(@LI_Input_Variable1_abcde)非常多,100 x LEN(@vXX) + 200 x LEN(@nXX)少得多 -
您的问题(堆栈跟踪)中只有一个小线索表明您使用的是 Mono,而不是 Microsoft 的 CLR。如果您在
mono sqlcommand index out of range上进行搜索,您会发现很多提示暗示 Mono 的SqlClient类的实现可能存在连接池和/或线程问题。
标签: sql sql-server unity3d monodevelop executenonquery