DocumentClient.CreateDatabaseAsync(Database, RequestOptions) DocumentClient.CreateDatabaseAsync(Database, RequestOptions) Method

Definition

Creates a database resource as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>> CreateDatabaseAsync (Microsoft.Azure.Documents.Database database, Microsoft.Azure.Documents.Client.RequestOptions options = null);
abstract member CreateDatabaseAsync : Microsoft.Azure.Documents.Database * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>>
override this.CreateDatabaseAsync : Microsoft.Azure.Documents.Database * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>>
Parameters
database
Database Database

The specification for the Database to create.

options
RequestOptions RequestOptions

(Optional) The RequestOptions for the request.

Returns

The Database that was created within a task object representing the service response for the asynchronous operation.

Implements
Exceptions

Represents a consolidation of failures that occured during async processing. Look within InnerExceptions to find the actual exception(s).

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Database are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the database object supplied. It is likely that an id was not supplied for the new Database.
409Conflict - This means a Database with an id matching the id field of database already existed.

Examples

The example below creates a new Database with an Id property of 'MyDatabase' This code snippet is intended to be used from within an asynchronous method as it uses the await keyword


using (DocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
Database db = await client.CreateDatabaseAsync(new Database { Id = "MyDatabase" });
}

If you would like to construct a Database from within a synchronous method then you need to use the following code


using (DocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
Database db = client.CreateDatabaseAsync(new Database { Id = "MyDatabase" }).Result;
}

Applies to

See Also