Documentdb 简明教程
DocumentDB - Create Database
在本章中,我们将学习如何新建一个数据库。要使用 Microsoft Azure DocumentDB,你必须有一个 DocumentDB 帐户、一个数据库、一个集合和文档。我们已经有了 DocumentDB 帐户,现在我们有两种选择来创建数据库 −
In this chapter, we will learn how to create a database. To use Microsoft Azure DocumentDB, you must have a DocumentDB account, a database, a collection, and documents. We already have a DocumentDB account, now to create database we have two options −
-
Microsoft Azure Portal or
-
.Net SDK
Create a Database for DocumentDB using the Microsoft Azure Portal
若要使用门户创建数据库,以下为步骤。
To create a database using portal, following are the steps.
Step 1 − 登录到 Azure 门户,你将看到仪表板。
Step 1 − Login to Azure portal and you will see the dashboard.

Step 2 − 现在单击已创建的 DocumentDB 帐户,你将看到详细信息,如下图所示。
Step 2 − Now click on the created DocumentDB account and you will see the details as shown in the following screenshot.

Step 3 − 选择“添加数据库”选项并提供数据库 ID。
Step 3 − Select the Add Database option and provide the ID for your database.

Step 4 - 单击“确定”。
Step 4 − Click OK.

您会看到添加了数据库。目前,尚未添加任何集合,但稍后我们可以添加将用于存储 JSON 文档的容器集合。请注意,它同时具有 ID 和资源 ID。
You can see that the database is added. At the moment, it has no collection, but we can add collections later which are the containers that will store our JSON documents. Notice that it has both an ID and a Resource ID.
Create a Database for DocumentDB Using .Net SDK
如需使用 .Net SDK 创建数据库,请执行以下步骤。
To create a database using .Net SDK, following are the steps.
Step 1 − 从上一章中在 Visual Studio 中打开控制台应用程序。
Step 1 − Open the Console Application in Visual Studio from the last chapter.
Step 2 − 创建新的数据库对象来创建新数据库。如需创建新数据库,我们只需分配 Id 属性,我们将其设置为 CreateDatabase 任务中的“mynewdb”。
Step 2 − Create the new database by creating a new database object. To create a new database, we only need to assign the Id property, which we are setting to “mynewdb” in a CreateDatabase task.
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 *******");
}
Step 3 − 现在将此 databaseDefinition 传递给 CreateDatabaseAsync,然后以资源属性的形式获取结果。所有创建对象的方法都返回一个描述已创建项(在本例中为数据库)的资源属性。
Step 3 − Now pass this databaseDefinition on to CreateDatabaseAsync, and get back a result with a Resource property. All the create object methods return a Resource property that describes the item that was created, which is a database in this case.
从资源属性中获取新的数据库对象,并将其与 DocumentDB 分配给它的资源 ID 一起显示在控制台上。
We get the new database object from the Resource property and it is displayed on the Console along with the Resource ID that DocumentDB assigned to it.
Step 4 − 现在,在实例化 DocumentClient 后,从 CreateDocumentClient 任务调用 CreateDatabase 任务。
Step 4 − Now call CreateDatabase task from the CreateDocumentClient task after DocumentClient is instantiated.
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(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);
}
}
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。
When the above code is compiled and executed, you will receive the following output which contains the Database and Resources IDs.
******** Create Database *******
Database Id: mynewdb; Rid: ltpJAA==
******** Database Created *******