Uredi

Deli z drugimi prek


SqlConnection.PacketSize Property

Definition

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

Property Value

The size (in bytes) of network packets. The default value is 8000.

Attributes

Examples

The following example creates a SqlConnection, including setting the Packet Size to 512 in the connection string. 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

Remarks

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. 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 may be a value in the range of 512 and 32767 bytes. An exception is generated if the value is outside this range.

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. For more information on how SQL Server uses memory, see Memory Management Architecture Guide.

Applies to

See also