SqlCommand查询的最大长度是多少?

3

我目前正在使用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中实现此脚本。

5
我认为这不是“参数长度”问题。一定有某个“参数”类似于数组,导致了“索引越界异常”。没有更多细节,很难回答实际引起问题的原因。 - Guruprasad J Rao
1
如果你不得不问……那就太长了 :) - Jeremy
1
请不要只给我们一堆冗长的代码 - 请确保创建一个 [mcve]。侧重于最小化。 - Damien_The_Unbeliever
1
尝试使用更短的名称 :-) 100 x 3 x LEN(@LI_Input_Variable1_abcde)非常多的100 x LEN(@vXX) + 200 x LEN(@nXX)少得多 - Shnugo
2
你的问题只提供了一个小线索(堆栈跟踪),表明你在使用Mono而不是Microsoft的CLR。 如果您搜索“mono sqlcommand index out of range”,您会发现很多提示可能与Mono实现SqlClient类时存在的连接池和/或线程问题有关的匹配项。 - Damien_The_Unbeliever
显示剩余6条评论
2个回答

9

让我回答你提出的问题:SqlCommand查询的最大长度是多少?

语句的最大长度是65536 * 网络数据包大小,默认情况下约为1500字节。进行计算 - 大约为90MB。

超出这个长度不会导致错误,但这就是你所问的问题。


it is around 90 MB - Avrob

2
我一直在通过减少更新变量的数量来尝试找到限制,并且我发现SQL命令长度有一个甜点:
5,973个字符(不含空格) 7,967个字符(包含空格)
142个参数。
我认为在这种情况下,8000个字符可能是限制,但我无法百分之百保证。
我忘了说我正在使用集成在Unity3D中的MonoDevelop 5.9.6。另一个用户(Damien_The_Unbeliever)给了我线索,它可能是Mono实现SqlClient类时的连接池和/或线程问题。

1
你尝试使用更短的名称了吗?8000非常接近SQL Server VARCHAR(intValue)的最大长度。不知道Mono的特殊性,但字符串长度可能是一个问题...你可以在300个地方节省约20个字符,值得一试... - Shnugo
谢谢你的建议! 是的,我知道。问题是我不能缩短变量名。如果可以的话,我肯定会这样做,但它们必须保持原样。 - MetalxBeat
1
@MetalxBeat 这也发生在我身上了。我在想,不要传递太多参数,使用临时表来处理脚本逻辑,或许可以解决这个问题。 - FindOutIslamNow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接