Documentdb 简明教程
DocumentDB - List Databases
到目前为止,我们在 DocumentDB 帐户中创建了两个数据库,第一个是使用 Azure 门户创建的,而第二个是使用 .Net SDK 创建的。现在,您可以使用 Azure 门户查看这些数据库。
So far, we have created two databases in our DocumentDB account, first one is created using Azure portal while the second database is created using .Net SDK. Now to view these databases, you can use Azure portal.
转到 Azure 门户中的 DocumentDB 帐户,您现在会看到两个数据库。
Go to your DocumentDB account on Azure portal and you will see two databases now.

您还可以使用 .Net SDK 从代码中查看或列出数据库。以下为相关步骤。
You can also view or list the databases from your code using .Net SDK. Following are the steps involved.
Step 1 − 发出没有参数的数据库查询,该查询会返回完整的列表,但您也可以传入一个查询以查找特定的数据库或特定数据库。
Step 1 − Issue a database Query with no parameters which returns a complete list, but you can also pass in a query to look for a specific database or specific databases.
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);
}
您会看到,有一堆用于查找集合、文档、用户和其他资源的 CreateQuery 方法。这些方法实际上并不执行查询,它们只是定义查询并返回可迭代对象。
You will see that there are a bunch of these CreateQuery methods for locating collections, documents, users, and other resources. These methods don’t actually execute the query, they just define the query and return an iterateable object.
实际上执行查询、迭代结果并将它们作为列表返回的是对 ToList() 的调用。
It’s the call to ToList() that actually executes the query, iterates the results, and returns them in a list.
Step 2 − 在实例化 DocumentClient 之后,从 CreateDocumentClient 任务调用 GetDatabases 方法。
Step 2 − Call GetDatabases method from the CreateDocumentClient task after DocumentClient is instantiated.
Step 3 − 您还需要对 CreateDatabase 任务进行注释或更改数据库 ID,否则您会收到一个错误消息,指出数据库已存在。
Step 3 − You also need to comment the CreateDatabase task or change the database id, otherwise you will get an error message that the database exists.
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
//await CreateDatabase(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);
}
}
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);
}
}
}
当编译并执行上述代码时,您会收到以下输出,其中包含两个数据库的数据库和资源 ID。最后,您还将看到数据库的总数。
When the above code is compiled and executed you will receive the following output which contains the Database and Resources IDs of both the databases. In the end you will also see the total number of databases.
******** Get Databases List ********
Database Id: myfirstdb; Rid: Ic8LAA==
Database Id: mynewdb; Rid: ltpJAA==
Total databases: 2