Create a database (.NET)
- Your DocumentDB database can be created by using the CreateDatabaseAsync method of the DocumentClient class. A database is the logical container of JSON document storage partitioned across collections.
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
click below button to copy the code. By azure tutorial team
- To create your database:
async Task CreateDatabase(DocumentClient client)
{
var databaseName = "<your database name>";
await client.CreateDatabaseAsync(new Database { Id = databaseName });
}
click below button to copy the code. By azure tutorial team
- You can also check if the database already exists and create it if needed:
async Task CreateDatabaseIfNotExists(DocumentClient client)
{
var databaseName = "<your database name>";
try
{
await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
}
catch (DocumentClientException e)
{
// If the database does not exist, create a new database
if (e.StatusCode == HttpStatusCode.NotFound)
{
await client.CreateDatabaseAsync(new Database { Id = databaseName });
}
else
{
// Rethrow
throw;
}
}
}