DocumentDB - 建立資料庫



本章我們將學習如何建立一個數據庫。要使用 Microsoft Azure DocumentDB,您必須擁有一個 DocumentDB 帳戶、一個數據庫、一個集合和文件。我們已經擁有一個 DocumentDB 帳戶,現在要建立資料庫,我們有兩個選項:

  • Microsoft Azure 門戶 或
  • .Net SDK

使用 Microsoft Azure 門戶為 DocumentDB 建立資料庫

要使用門戶建立資料庫,請按照以下步驟操作。

步驟 1 - 登入 Azure 門戶,您將看到儀表板。

Login to portal

步驟 2 - 現在單擊已建立的 DocumentDB 帳戶,您將看到如下螢幕截圖所示的詳細資訊。

click on created DocumentDB

步驟 3 - 選擇“新增資料庫”選項,併為您的資料庫提供 ID。

Select Add Database

步驟 4 - 單擊“確定”。

Database added

您可以看到資料庫已新增。目前,它沒有任何集合,但我們稍後可以新增集合,這些集合將儲存我們的 JSON 文件。請注意,它同時具有 ID 和資源 ID。

使用 .Net SDK 為 DocumentDB 建立資料庫

要使用 .Net SDK 建立資料庫,請按照以下步驟操作。

步驟 1 - 從上一章開啟 Visual Studio 中的控制檯應用程式。

步驟 2 - 透過建立一個新的資料庫物件來建立新的資料庫。要建立新的資料庫,我們只需要分配 Id 屬性,在 CreateDatabase 任務中將其設定為“mynewdb”。

private async static Task CreateDatabase(DocumentClient client) {
   Console.WriteLine(); 
   Console.WriteLine("******** Create Database *******");
	
   var databaseDefinition = new Database { Id = "mynewdb" }; 
   var result = await client.CreateDatabaseAsync(databaseDefinition); 
   var database = result.Resource;
	
   Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId); 
   Console.WriteLine("******** Database Created *******"); 
} 

步驟 3 - 現在將此 databaseDefinition 傳遞給 CreateDatabaseAsync,並獲取具有 Resource 屬性的結果。所有 create 物件方法都返回一個 Resource 屬性,該屬性描述已建立的專案,在本例中為資料庫。

我們從 Resource 屬性獲取新的資料庫物件,並將其與 DocumentDB 為其分配的資源 ID 一起顯示在控制檯上。

步驟 4 - 在例項化 DocumentClient 後,從 CreateDocumentClient 任務呼叫 CreateDatabase 任務。

using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) { 
   await CreateDatabase(client); 
} 

以下是到目前為止完整的 Program.cs 檔案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;

namespace DocumentDBDemo {

   class Program {
      private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
		
      private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
         StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
			
      static void Main(string[] args) {
         try {
            CreateDocumentClient().Wait();
         } catch (Exception e) {
            Exception baseException = e.GetBaseException();
            Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
         }
         Console.ReadKey();
      }
		
      private static async Task CreateDocumentClient() {
         // Create a new instance of the DocumentClient
         using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
            await CreateDatabase(client);
         } 
      }
		
      private async static Task CreateDatabase(DocumentClient client) {
         Console.WriteLine();
         Console.WriteLine("******** Create Database *******");
			
         var databaseDefinition = new Database { Id = "mynewdb" };
         var result = await client.CreateDatabaseAsync(databaseDefinition);
         var database = result.Resource;
			
         Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId);
         Console.WriteLine("******** Database Created *******");
      }
		
   } 
}

編譯並執行上述程式碼後,您將收到以下輸出,其中包含資料庫和資源 ID。

******** Create Database ******* 
 Database Id: mynewdb; Rid: ltpJAA== 
******** Database Created ******* 
廣告