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

标签 sql sql-server unity3d monodevelop executenonquery

我目前正在使用 SQL Server 2014 中的 C# 脚本更新和读取值。当使用 SQlCommand 执行非查询时,运行脚本时会弹出错误:

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)

我数了一下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 中

最佳答案

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

语句的最大大小为 65536*Network Packet Size - 默认情况下约为 1500 字节。算一下 - 大约是 90MB。

超过这个,虽然不会导致你的错误。但这是你提出的问题。

关于sql - SqlCommand 查询的最大长度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353208/

相关文章:

SQL 服务器 : SELECT rows using ROW_NUMBER and CASE statement

c# - Microsoft.SqlServer.Management.Smo 获取 SQL Server 的恢复模型

mysql - 在 MySQL 中,如何在 FORMAT() 之后按数字顺序对字段进行排序?

sql - 如何检索一个月后年龄为 21 岁的所有员工?

sql-server - 无法在 SQL Server 2012 中编辑 SQL 代理作业步骤

sql - 从动态sql中删除最后一个逗号

asp.net - Windows Server的Web搜索引擎是开放源代码/免费的

unity3d - 多声道音频输入统一

c# - 我可以在 Unity 中使用预处理器指令来区分调试和 Release模式吗

unity3d - 构建 .exe(UNITY) 时相机处于不同位置