SqlConnection.PacketSize SqlConnection.PacketSize SqlConnection.PacketSize SqlConnection.PacketSize Property

定义

获取用于与 SQL Server 的实例进行通信的网络数据包的大小(以字节为单位)。Gets the size (in bytes) of network packets used to communicate with an instance of SQL Server.

public:
 property int PacketSize { int get(); };
[System.Data.DataSysDescription("SqlConnection_PacketSize")]
public int PacketSize { get; }
member this.PacketSize : int
Public ReadOnly Property PacketSize As Integer

属性值

网络数据包的大小(以字节为单位)。The size (in bytes) of network packets. 默认值为 8000。The default value is 8000.

示例

下面的示例创建一个SqlConnection,包括在连接Packet Size字符串中将设置为512。The following example creates a SqlConnection, including setting the Packet Size to 512 in the connection string. 它在控制台PacketSize窗口ServerVersion中显示和属性。It displays the PacketSize and ServerVersion properties in the console window.

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("PacketSize: {0}", connection.PacketSize);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file, using the 
    // System.Configuration.ConfigurationSettings.AppSettings property 
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;Packet Size=512";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
        Console.WriteLine("PacketSize: {0}", connection.PacketSize)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' To avoid storing the connection string in your code,  
    ' you can retrieve it from a configuration file, using the
    ' System.Configuration.ConfigurationSettings.AppSettings property
    Return "Data Source=(local);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;Packet Size=512;"
End Function

注解

如果应用程序执行大容量复制操作,或者发送或接收大量文本或图像数据,则大于默认值的数据包大小可能会提高效率,因为它会导致网络读写操作更少。If an application performs bulk copy operations, or sends or receives lots of text or image data, a packet size larger than the default may improve efficiency because it causes fewer network read and write operations. 如果应用程序发送和接收的信息量较少,则可以将数据包大小设置为512字节(使用中ConnectionString的 "数据包大小" 值),这对大多数数据传输操作来说已经足够了。If an application sends and receives small amounts of information, you can set the packet size to 512 bytes (using the Packet Size value in the ConnectionString), which is sufficient for most data transfer operations. 对于大多数应用程序而言,默认数据包大小是最佳的。For most applications, the default packet size is best.

PacketSize可能为512到32767字节范围内的值。PacketSize may be a value in the range of 512 and 32767 bytes. 如果值超出此范围,则会生成异常。An exception is generated if the value is outside this range.

如果将默认值设置为大于8000的数字,则数据包将使用实例上的多页分配器 SQL Server 而不是更有效的 SinglePage 分配器,从而降低 SQL Server 的整体可伸缩性。Setting the default value to a number greater than 8000 will cause the packets to use the MultiPage allocator on the instance of SQL Server instead of the much more efficient SinglePage allocator, reducing the overall scalability of the SQL Server. 有关 SQL Server 如何使用内存的详细信息,请参阅内存管理体系结构指南For more information on how SQL Server uses memory, see Memory Management Architecture Guide.

适用于

另请参阅