コード例 #1
0
        /// <summary>
        /// Run basic database meta data operations as a console application.
        /// </summary>
        /// <returns></returns>
        private static async Task RunDatabaseDemo(CosmosClient client)
        {
            // An object containing relevant information about the response
            DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync(databaseId, 10000);

            // A client side reference object that allows additional operations like ReadAsync
            CosmosDatabase database = databaseResponse;

            // The response from Azure Cosmos
            CosmosDatabaseSettings settings = databaseResponse;

            Console.WriteLine($"\n1. Create a database resource with id: {settings.Id} and last modified time stamp: {settings.LastModified}");
            Console.WriteLine($"\n2. Create a database resource request charge: {databaseResponse.RequestCharge} and Activity Id: {databaseResponse.ActivityId}");

            // Read the database from Azure Cosmos
            DatabaseResponse readResponse = await database.ReadAsync();

            Console.WriteLine($"\n3. Read a database: {readResponse.Resource.Id}");

            await readResponse.Database.CreateContainerAsync("testContainer", "/pk");

            // Get the current throughput for the database
            int?throughput = await database.ReadProvisionedThroughputAsync();

            if (throughput.HasValue)
            {
                Console.WriteLine($"\n4. Read a database throughput: {throughput.Value}");

                // Update the current throughput for the database
                await database.ReplaceProvisionedThroughputAsync(11000);
            }

            Console.WriteLine("\n5. Reading all databases resources for an account");
            FeedIterator <CosmosDatabaseSettings> iterator = client.GetDatabasesIterator();

            do
            {
                foreach (CosmosDatabaseSettings db in await iterator.FetchNextSetAsync())
                {
                    Console.WriteLine(db.Id);
                }
            } while (iterator.HasMoreResults);

            // Delete the database from Azure Cosmos.
            await database.DeleteAsync();

            Console.WriteLine($"\n6. Database {database.Id} deleted.");
        }