SqlConnection.PacketSize 属性

定义

获取用于与 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(); };
public int PacketSize { get; }
[System.Data.DataSysDescription("SqlConnection_PacketSize")]
public int PacketSize { get; }
member this.PacketSize : int
[<System.Data.DataSysDescription("SqlConnection_PacketSize")>]
member this.PacketSize : int
Public ReadOnly Property PacketSize As Integer

属性值

Int32

网络数据包的大小(以字节为单位)。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.

适用于