共用方式為


使用 SqlTrackingService 進行資料維護

Windows Workflow Foundation 提供用於 SqlTrackingService 的資料維護功能。 此功能可讓您在完成工作流程執行個體時分割追蹤資料。 分割包括將插入的追蹤資料,從已完成執行的工作流程執行個體移到已知為分割的個別資料庫資料表中。 Windows Workflow Foundation 提供兩個用於分割的個別配置,讓您可以選擇較適用於您的工作流程方案的配置:

  • 完成時分割

  • 隨選分割

完成時分割

完成時分割是針對沒有停機時間和不想要有停機時間的應用程式而設計。 在完成每個工作流程執行個體時完成分割。 產生自追蹤工作流程執行個體的記錄新增至定期追蹤資料表中,直列工作流程執行個體完成為止。 那時,記錄移動至分割資料表中。 啟用分割時,定期建立一組用於追蹤資料的新資料表。 完成的工作流程執行個體的追蹤資料移到這些新分割中,而且不用中斷目前執行中的應用程式。

設定完成時分割

根據預設,未開啟用於 SqlTrackingService 的分割。 若要啟用 SqlTrackingService 管理的分割,請將 PartitionOnCompletion 屬性設定為如下列程式碼範例所示的 true

[C#]

string connectionString = "Initial Catalog=Tracking;Data Source=localhost;Integrated Security=SSPI;";
SqlTrackingService sqlTrackingService = new SqlTrackingService(connectionString);
sqlTrackingService.PartitionOnCompletion = true;

當您使用 PartitionOnCompletion 屬性時,您可以設定分割資料表,以根據時間間隔建立新的分割資料表。 變更 SqlTrackingService 配置中的 TrackingPartitionInterval.Interval 欄來完成此工作。 名為 SetPartitionInterval 的預存程序可讓您以程式設計方法來執行此工作,以在應用程式執行期間自訂時間間隔。 預設的分割時間間隔是每月 (以「m」表示)。 可用值包括每日 (以「d」表示) 和每年 (以「y」表示)。 啟用之後,Windows Workflow Foundation 根據您指定的時間間隔建立新的分割集 (新資料表集)。 資料表 vw_TrackingPartitionSetName 含有關於分割集名稱的資訊,包括 Name yyyy_mCreatedDateTimeEndDateTimePartitionInterval。 工作流程執行個體結束後,相對應的追蹤資料將移至新建立的資料表集。 下列程式碼範例示範如何設定使用 PartitionOnCompletion 配置時的分割間隔。

[C#]

internal static void SetPartitionInterval()
{
    // Valid values are 'd' (daily), 'm' (monthly), and 'y' (yearly).  
    // The default is 'm'.
    SqlCommand command = new SqlCommand("dbo.SetPartitionInterval");
    command.CommandType = CommandType.StoredProcedure;
    command.Connection = new SqlConnection(connectionString);
            
    SqlParameter intervalParameter = new SqlParameter("@Interval", SqlDbType.Char);
    intervalParameter.SqlValue = 'd';
    command.Parameters.Add(intervalParameter);
    try
    {
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        if ((command != null) && (command.Connection != null) && 
            (ConnectionState.Closed != command.Connection.State))
        {
            command.Connection.Close();
        }
        command.Dispose();
    }
}

隨選分割

隨選分割是針對定義或持續有停機時間,並且想要在該停機時間中進行分割的應用程式而設計。 若要啟用此配置,請建立執行 PartitionCompletedWorkflowInstances 預存程序的工作,這個程序在您執行 SqlTracking_Logic.sql 指令碼時建立。 執行此預存程序時,由完成的工作流程執行個體插入的追蹤資料從存留資料表移到分割資料表中。

顯示追蹤分割資料和資料表

下列程式碼說明如何擷取分割資訊,包括來自 SqlTrackingService 資料庫的已建立資料表。

[C#]

internal static void ShowTrackingPartitionInformation()
{
    // Show the contents of the TrackingPartitionName table.

    SqlCommand command = new SqlCommand("SELECT * FROM vw_TrackingPartitionSetName");
    SqlDataReader reader = null;
    command.CommandType = CommandType.Text;
    command.Connection = new SqlConnection(connectionString);

    try
    {
        command.Connection.Open();
        reader = command.ExecuteReader();
        if (reader.Read())
        {
            Console.WriteLine();
            Console.WriteLine("***************************");
            Console.WriteLine("Partition information: ");
            Console.WriteLine("PartitionId: {0}", reader[0]);
            Console.WriteLine("Name: {0}", reader[1]);
            Console.WriteLine("Created: {0}", reader[2]);
            Console.WriteLine("End: {0}", reader[3] is System.DBNull ? "NULL" : reader[3]);
            Console.WriteLine("Partition Interval: {0}", reader[4]);
            Console.WriteLine("***************************");
        }
        else
        {
            Console.WriteLine();
            Console.WriteLine("No partition information present.");
        }

    }
    finally
    {
        if ((reader != null) && (!reader.IsClosed))
            reader.Close();
        if ((command != null) && (command.Connection != null) && (ConnectionState.Closed != command.Connection.State))
        {
            command.Connection.Close();
        }
        command.Dispose();
    }
}

下列程式碼範例示範如何顯示在 SqlTrackingService 資料庫中的分割資料表名稱。

internal static void ShowPartitionTableInformation()
{
    SqlCommand command = new SqlCommand(
        "declare @trackingName varchar(255) select @trackingName = Name from vw_TrackingPartitionSetName " +
        "select name from sysobjects where name like '%' + @trackingName");
    
    Console.WriteLine();
    Console.WriteLine("***************************");
    Console.WriteLine("Partition tables: ");
    SqlDataReader reader = null;
    command.CommandType = CommandType.Text;
    command.Connection = new SqlConnection(connectionString);

    try
    {
        command.Connection.Open();
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(reader[0]);
        }
    }
    finally
    {
        if ((reader != null) && (!reader.IsClosed))
            reader.Close();
        if ((command != null) && (command.Connection != null) && (ConnectionState.Closed != command.Connection.State))
        {
            command.Connection.Close();
        }
        command.Dispose();
    }
    Console.WriteLine("***************************");
}

請參閱

參考

SqlTrackingService
PartitionOnCompletion

概念

使用 SqlTrackingService
使用 SqlTrackingQuery 查詢 SqlTrackingService 資料
Windows 工作流程追蹤服務

其他資源

SQL Data Maintenance Sample
Query Using SQLTrackingService Sample

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.