

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 .NET 連線至 Neptune 資料庫執行個體
<a name="access-graph-gremlin-dotnet"></a>

**重要**  
選擇正確的 Apache TinkerPop Gremlin 驅動程式版本對於與您的 Neptune 引擎版本相容至關重要。使用不相容的版本可能會導致連線失敗或非預期的行為。如需詳細的版本相容性資訊，請參閱 [使用 Gremlin 存取 Neptune 圖形](access-graph-gremlin.md)。

下節包含以 C\# 撰寫的程式碼範例，其會連線至 Neptune 資料庫執行個體並執行 Gremlin 周遊。

必須從與您的 Neptune 資料庫執行個體位於同一虛擬私有雲端 (VPC) 的 Amazon EC2 執行個體連線至 Amazon Neptune。此範本程式碼已在執行 Ubuntu 的 Amazon EC2 執行個體上測試。

開始之前，請執行以下動作：
+ 在 Amazon EC2 執行個體上安裝 .NET。如需在包括 Windows、Linux 和 macOS 等多重作業系統上安裝 .NET 的指示，請參閱[開始使用 .NET](https://www.microsoft.com/net/learn/get-started/)。
+ 請執行套件的 `dotnet add package gremlin.net`以安裝 Gremlin.NET。如需詳細資訊，請參閱 TinkerPop 文件的 [Gremlin.NET](https://tinkerpop.apache.org/docs/current/reference/#gremlin-DotNet)。



**使用 Gremlin.NET 連線至 Neptune**

1. 建立新的 .NET 專案。

   ```
   dotnet new console -o gremlinExample
   ```

1. 將目錄變更為新的專案目錄。

   ```
   cd gremlinExample
   ```

1. 將以下內容複製到 `Program.cs` 檔案。將 {{your-neptune-endpoint}} 取代為 Neptune 資料庫執行個體的地址。

   如需尋找 Neptune 資料庫執行個體地址的相關資訊，請參閱 [連線至 Amazon Neptune 端點](feature-overview-endpoints.md) 一節。

   ```
   using System;
   using System.Threading.Tasks;
   using System.Collections.Generic;
   using Gremlin.Net;
   using Gremlin.Net.Driver;
   using Gremlin.Net.Driver.Remote;
   using Gremlin.Net.Structure;
   using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource;
   namespace gremlinExample
   {
     class Program
     {
       static void Main(string[] args)
       {
         try
         {
           var endpoint = "your-neptune-endpoint";
           // This uses the default Neptune and Gremlin port, 8182
           var gremlinServer = new GremlinServer(endpoint, 8182, enableSsl: true );
           var gremlinClient = new GremlinClient(gremlinServer);
           var remoteConnection = new DriverRemoteConnection(gremlinClient, "g");
           var g = Traversal().WithRemote(remoteConnection);
           g.AddV("Person").Property("Name", "Justin").Iterate();
           g.AddV("Custom Label").Property("name", "Custom id vertex 1").Iterate();
           g.AddV("Custom Label").Property("name", "Custom id vertex 2").Iterate();
           var output = g.V().Limit<Vertex>(3).ToList();
           foreach(var item in output) {
               Console.WriteLine(item);
           }
         }
         catch (Exception e)
         {
             Console.WriteLine("{0}", e);
         }
       }
     }
   }
   ```

1. 輸入下列命令以執行範例：

   ```
   dotnet run
   ```

   本範例結尾的 Gremlin 查詢會傳回單一頂點的數量，以進行測試。接著會列印至主控台。
**注意**  
Gremlin 查詢最後的部分 `Next()` 用來提交周遊至伺服器，以供進行評估。如果您未包含該方法或其他同等方法，該查詢不會提交到 Neptune 資料庫執行個體。

   以下方法會查詢提交至 Neptune 資料庫執行個體：
   + `ToList()`
   + `ToSet()`
   + `Next()`
   + `NextTraverser()`
   + `Iterate()`

   如果您需要序列化並傳回查詢結果，請使用 `Next()`，或者如果不需要，則使用 `Iterate()`。

   上述範例會使用 `g.V().Limit(3).ToList()` 周遊傳回清單。若要查詢其他內容，將其換成其他使用其中一個適當之結束方法的 Gremlin 周遊。

## IAM 身分驗證
<a name="access-graph-gremlin-dotnet-iam"></a>

Neptune 支援 [IAM 身分驗證](iam-auth-enable.md)，以控制對資料庫叢集的存取。如果您已啟用 IAM 身分驗證，則需要使用 Signature 第 4 版簽署來驗證您的請求。如需從 .NET 用戶端連線的詳細說明和程式碼範例，請參閱 [使用 IAM 身分驗證搭配 Gremlin .NET 連線至 Amazon Neptune 資料庫](gremlin-dotnet-iam-auth.md)。