SqlConnection.PacketSize 屬性

定義

取得用來與 SQL Server 執行個體通訊的網路封包大小 (位元組)。

public:
 property int PacketSize { int get(); };
public int PacketSize { get; }
member this.PacketSize : int
Public ReadOnly Property PacketSize As Integer

屬性值

網路封包的大小 (位元組)。 預設值為 8000。

範例

下列範例會 SqlConnection 建立 ,包括將 設定 Packet Size 為連接字串中的 512。 它會在主控台視窗中顯示 PacketSizeServerVersion 屬性。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        OpenSqlConnection();
        Console.ReadLine();
    }

    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";
    }
}

備註

如果應用程式執行大量複製作業,或傳送或接收大量文字或影像資料,大於預設值的封包大小可能會提升效率,因為它會造成較少的網路讀取和寫入作業。 如果應用程式傳送和接收少量的資訊,您可以使用) 中的 ConnectionString 封包大小值,將封包大小設定為 512 位元組 (,這足以用於大部分的資料傳輸作業。 對於大部分應用程式而言,預設封包大小是最適當的大小。

PacketSize 可以是 512 和 32767 位元組範圍內的值。 如果值超出這個範圍,就會產生例外狀況。

將預設值設定為大於 8000 的數位,會導致封包在 SQL Server 實例上使用 MultiPage 配置器,而不是更有效率的 SinglePage 配置器,以減少SQL Server的整體延展性。 如需SQL Server如何使用記憶體的詳細資訊,請參閱記憶體管理架構指南

適用於