In this chapter, we are able to learn how to create a database. To use Microsoft Azure DocumentDB, you ought to have a DocumentDB account, a database, a group, and documents. We already have a DocumentDB account, now to create database we've two alternatives −
- Microsoft Azure Portal or
- .Net SDK
Create a Database for DocumentDB the usage of the Microsoft Azure Portal
To create a database the usage of portal, following are the stairs.
Step 1 − Login to Azure portal and you will see the dashboard.
Step 2 − Now click on on the created DocumentDB account and you may see the information as proven within the following screenshot.
Step 3 − Select the Add Database choice and provide the ID on your database.
Step 4− Click OK.
You can see that the database is brought. At the moment, it has no series, but we will upload collections later which are the containers in order to save our JSON files. Notice that it has each an ID and a Resource ID.
Create a Database for DocumentDB Using .Net SDK
To create a database the use of .Net SDK, following are the steps.
Step 1 − Open the Console Application in Visual Studio from the ultimate chapter.
Step 2 − Create the brand new database with the aid of growing a brand new database item. To create a brand new database, we best want to assign the Id property, which we are setting to “mynewdb” in a CreateDatabase undertaking.
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 − Now bypass this databaseDefinition directly to CreateDatabaseAsync, and get returned a result with a Resource belongings. All the create object strategies return a Resource property that describes the object that was created, that's a database in this case.
We get the new database object from the Resource assets and it's far displayed at the Console along side the Resource ID that DocumentDB assigned to it.
Step 4− Now call CreateDatabase undertaking from the CreateDocumentClient mission after DocumentClient is instantiated.
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(client);
}
Following is the whole Program.Cs record 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 *******");
}
}
}
When the above code is compiled and carried out, you may obtain the following output which includes the Database and Resources IDs.
******** Create Database *******
Database Id: mynewdb; Rid: ltpJAA==
******** Database Created *******