DocumentClient.CreateDatabaseIfNotExistsAsync メソッド

定義

Azure Cosmos DB サービスの非同期操作として、データベース リソースを作成 (存在しない場合) または gets(既に存在する場合) を作成します。 応答から状態コードをチェックして、データベースが新しく作成されたか(201)、または既存のデータベースが返されたのかを判断できます(200)

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>> CreateDatabaseIfNotExistsAsync (Microsoft.Azure.Documents.Database database, Microsoft.Azure.Documents.Client.RequestOptions options = default);
abstract member CreateDatabaseIfNotExistsAsync : Microsoft.Azure.Documents.Database * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>>
override this.CreateDatabaseIfNotExistsAsync : Microsoft.Azure.Documents.Database * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>>
Public Function CreateDatabaseIfNotExistsAsync (database As Database, Optional options As RequestOptions = Nothing) As Task(Of ResourceResponse(Of Database))

パラメーター

database
Database

作成する の Database 仕様。

options
RequestOptions

(省略可能) RequestOptions 要求の 。

戻り値

非同期操作の Database サービス応答を表すタスク オブジェクト内に作成された 。

実装

例外

が設定されていない場合 database

非同期処理中に発生したエラーの統合を表します。 InnerExceptions 内を見て、実際の例外を見つけます。

この例外は、さまざまな種類のエラーをカプセル化できます。 特定のエラーを特定するには、常に StatusCode プロパティを参照してください。

次の例では、Id プロパティが 'MyDatabase' の新しい Database を作成します。このコード スニペットは、await キーワード (keyword)を使用するため、非同期メソッド内から使用することを目的としています。

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

同期メソッド内から を Database 構築する場合は、次のコードを使用する必要があります。

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

適用対象

こちらもご覧ください