> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 將 Kusto C# SDK 連線至 Insights

> 使用 Kusto C# SDK 搭配 Microsoft Entra ID 驗證，從 Azure Functions 或自訂 .NET 應用程式查詢舊版 PlayFab Insights 遊戲資料。

# 教學課程：將 Kusto C# SDK 連線至 Insights

本指南可協助您開始搭配 Insights 使用 Kusto C# SDK。連線之後，您可以從 Azure 函式查詢您的 Insights。若要深入了解可與 Insights 連線的其他工具，請參閱[將外部工具連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity)。

<Note>
  PlayFab Insights 管理已於 3/31/2026 淘汰。我們建議日後使用 [Azure Data Explorer (ADX) 連線](/zh-TW/services/playfab/data-analytics/export-data/data-connection-adx)來管理您的效能和成本。如果您的遊戲仍在使用 **Insights**，請繼續參閱本文以了解實作詳細資料。如需詳細資訊，請參閱 [PlayFab Digest: March feature updates](https://developer.microsoft.com/en-us/games/articles/2026/04/playfab-digest-march-feature-updates/)。
</Note>

## 必要條件

### 使用 AAD 驗證的 PlayFab 帳戶

您需要一個驗證提供者設定為 Microsoft 的 PlayFab 帳戶或使用者。Microsoft 驗證提供者使用 Azure Active Directory (AAD) 進行驗證，這是使用 Azure 服務的必要條件。如需建立 AAD 驗證帳戶或使用者的指示，請參閱 [Game Manager 的 Azure Active Directory 驗證](/zh-TW/services/playfab/identity/dev-identity/authentication/aad-authentication)。

若要確認帳戶或使用者已設定為使用 Microsoft 驗證提供者：

* 造訪 [developer.playfab.com](https://developer.playfab.com)。
* 選取 **Sign in with Microsoft** 以存取您的 PlayFab 帳戶。

如果您可以登入，表示該帳戶已設定為使用 Microsoft 驗證提供者。

### Insights 的 Game Manager 權限

您需要為帳戶指派已啟用下列 Game Manager 權限的[使用者角色](/zh-TW/services/playfab/identity/dev-identity/permissions/playfab-user-roles)：

* 系統管理員狀態。
* 存取 Explorer 索引標籤及相關資料。
* Analytics 資料的讀取和寫入權限。

您可以建立新的使用者角色，或將這些權限新增至現有角色。

### 其他必要條件

* [建立 Azure Active Directory (AAD) 應用程式並將其連線至您的遊戲資料庫](/zh-TW/services/playfab/data-analytics/legacy/connectivity/creating-AAD-app-for-insights)

## 安裝套件

1. 安裝下列 NuGet 套件：
   * 必要：[Microsoft.Azure.Kusto.Data](https://www.nuget.org/packages/Microsoft.Azure.Kusto.Data/)
   * 選用：[Microsoft.Azure.Kusto.Ingest](https://www.nuget.org/packages/Microsoft.Azure.Kusto.Ingest/)
   * 如需更多選用套件，請參閱 [Kusto .NET SDK 文件](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/netfx/about-the-sdk)
2. 以下是一些可協助您入門的範例程式碼。

```csharp theme={null}
namespace HelloPlayFabInsights
{
    using System;
    using Kusto.Data;
    using Kusto.Data.Common;
    using Kusto.Data.Net.Client;

    class Program
    {
        const string Cluster = "https://insights.playfab.com";
        const string Database = "<title id>";
        const string AzureAuthority = "microsoft.onmicrosoft.com";
        const string ClientId = "<app id>";
        const string ClientSecret = "<app secret>";

        static void Main()
        {
            var kcsb = new KustoConnectionStringBuilder(Cluster, Database).WithAadApplicationKeyAuthentication(ClientId, ClientSecret, AzureAuthority);

            Console.WriteLine("Run Query...");
            RunQuery(kcsb);

            Console.WriteLine("Run Command...");
            RunCommand(kcsb);
            
        }
    
        /// <summary>
        /// Run a query on your Insights database, and write the results to the console.
        /// </summary>
        /// <param name="kcsb"></param>
        static void RunQuery(KustoConnectionStringBuilder kcsb)
        {
            using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb))
            {
                var query = "['events.all'] | limit 10";

                var clientRequestProperties = new ClientRequestProperties() {
                    Application = "DotNetSDK",
                    ClientRequestId = Guid.NewGuid().ToString() 
                };
                using (var reader = queryProvider.ExecuteQuery(query, clientRequestProperties))
                { 
                    while (reader.Read())
                    {
                        string name = reader.GetString(2);
                        string titleId = reader.GetString(5);
                        DateTime timestamp = reader.GetDateTime(8);
                        Console.WriteLine("{0}\t{1}\t{2}", name, titleId, timestamp);
                    }
                }
            }
        }

        /// <summary>
        /// Run the ".show tables" command on your Insights database, and write the results to the console.
        /// </summary>
        /// <param name="kcsb"></param>
        static void RunCommand(KustoConnectionStringBuilder kcsb)
        {
            using (var commandProvider = KustoClientFactory.CreateCslAdminProvider(kcsb))
            {
                var command = ".show tables";

                var clientRequestProperties = new ClientRequestProperties()
                {
                    Application = "DotNetSDK",
                    ClientRequestId = Guid.NewGuid().ToString()
                };
                using (var reader = commandProvider.ExecuteControlCommand(command, clientRequestProperties))
                {
                    while (reader.Read())
                    {
                        string tableName = reader.GetString(0);
                        string databaseName = reader.GetString(1);
                        Console.WriteLine("{0}\t{1}", tableName, databaseName);
                    }
                }
            }
        }
    }
}
```

## 其他資源

* [Azure Kusto .NET 範例](https://github.com/Azure/azure-kusto-samples-dotnet)
* [Kusto 用戶端程式庫文件](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/netfx/about-kusto-data)


## Related topics

- [將外部工具與 Insights 連線](/zh-TW/services/playfab/data-analytics/legacy/connectivity/index.md)
- [將 Kusto Explorer 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-explorer-to-insights.md)
- [將 Python 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-python-to-insights.md)
- [將 Grafana 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-grafana-to-insights.md)
- [將 Azure Data Explorer 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-azure-data-explorer-to-insights.md)
