Documentdb 简明教程

DocumentDB - Drop Databases

您可以通过门户或代码(使用 .Net SDK)删除数据库。在这里,我们将按部就班地讨论如何在 DocumentDB 中删除数据库。

You can drop a database or databases from the portal as well as from the code by using .Net SDK. Here, we will discuss, in a step-wise manner, how to drop a database in DocumentDB.

Step 1 − 在 Azure 门户上转到 DocumentDB 帐户。为了演示,我添加了另外两个数据库,如下图所示。

Step 1 − Go to your DocumentDB account on Azure portal. For the purpose of demo, I have added two more databases as seen in the following screenshot.

drop databases

Step 2 − 若要删除任何数据库,您需要单击该数据库。我们选择 tempdb,您将看到以下页面,选择“删除数据库”选项。

Step 2 − To drop any database, you need to click that database. Let’s select tempdb, you will see the following page, select the ‘Delete Database’ option.

delete database

Step 3 − 它将显示确认信息,现在单击“是”按钮。

Step 3 − It will display the confirmation message, now click the ‘Yes’ button.

confirmation message

您将看到 tempdb 不再出现在仪表板中。

You will see that the tempdb is no more available in your dashboard.

tempdb deleted

您还可以使用 .Net SDK 从代码中删除数据库。以下是要执行的步骤。

You can also delete databases from your code using .Net SDK. To do following are the steps.

Step 1 − 让我们通过指定我们想要删除的数据库的 ID 来删除该数据库,但是我们需要它的 SelfLink。

Step 1 − Let’s delete the database by specifying the ID of the database we want to delete, but we need its SelfLink.

Step 2 − 我们像之前一样调用 CreateDatabaseQuery,但这次我们实际上提供了一个查询,只返回 ID 为 tempdb1 的一个数据库。

Step 2 − We are calling the CreateDatabaseQuery like before, but this time we are actually supplying a query to return just the one database with the ID tempdb1.

private async static Task DeleteDatabase(DocumentClient client) {
   Console.WriteLine("******** Delete Database ********");
   Database database = client
      .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'tempdb1'")
      .AsEnumerable()
      .First();
   await client.DeleteDatabaseAsync(database.SelfLink);
}

Step 3 − 这一次,我们可以调用 AsEnumerable 而不是 ToList(),因为我们实际上不需要列表对象。预期仅一个结果,调用 AsEnumerable 就足够了,这样我们可以使用 First() 获得查询返回的第一个数据库对象。这是 tempdb1 的数据库对象,它具有 SelfLink,我们可以使用 SelfLink 调用 DeleteDatabaseAsync 来删除该数据库。

Step 3 − This time, we can call AsEnumerable instead of ToList() because we don’t actually need a list object. Expecting only result, calling AsEnumerable is sufficient so that we can get the first database object returned by the query with First(). This is the database object for tempdb1 and it has a SelfLink that we can use to call DeleteDatabaseAsync which deletes the database.

Step 4 − 您还需要在 DocumentClient 实例化后从 CreateDocumentClient 任务调用 DeleteDatabase 任务。

Step 4 − You also need to call DeleteDatabase task from the CreateDocumentClient task after DocumentClient is instantiated.

Step 5 − 若要查看删除指定数据库后的数据库列表,我们再次调用 GetDatabases 方法。

Step 5 − To view the list of databases after deleting the specified database, let’s call GetDatabases method again.

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

   GetDatabases(client);
   await DeleteDatabase(client);
   GetDatabases(client);
}

以下是迄今为止完整的 Program.cs 文件。

Following is the complete Program.cs file so far.

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);
            GetDatabases(client);
            await DeleteDatabase(client);
            GetDatabases(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 *******");
      }

      private static void GetDatabases(DocumentClient client) {
         Console.WriteLine();
         Console.WriteLine();
         Console.WriteLine("******** Get Databases List ********");

         var databases = client.CreateDatabaseQuery().ToList();

         foreach (var database in databases) {
            Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id,
               database.ResourceId);
         }

         Console.WriteLine();
         Console.WriteLine("Total databases: {0}", databases.Count);
      }

      private async static Task DeleteDatabase(DocumentClient client) {
         Console.WriteLine();
         Console.WriteLine("******** Delete Database ********");

         Database database = client
            .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'tempdb1'")
            .AsEnumerable()
            .First();
         await client.DeleteDatabaseAsync(database.SelfLink);
      }

   }
}

编译并执行上述代码后,您将收到以下输出,其中包含三个数据库的数据库和资源 ID 以及数据库总数。

When the above code is compiled and executed, you will receive the following output which contains the Database and Resources IDs of the three databases and total number of databases.

******** Get Databases List ********
 Database Id: myfirstdb; Rid: Ic8LAA==
 Database Id: mynewdb; Rid: ltpJAA==
 Database Id: tempdb1; Rid: 06JjAA==

Total databases: 3

******** Delete Database ********

******** Get Databases List ********
 Database Id: myfirstdb; Rid: Ic8LAA==
 Database Id: mynewdb; Rid: ltpJAA==

Total databases: 2

删除数据库后,您还将在最后看到 DocumentDB 帐户中只剩下两个数据库。

After deleting the database, you will also see at the end that only two databases are left in DocumentDB account.