Exemplo n.º 1
0
        // </Main>

        /// <summary>
        /// Run basic database meta data operations as a console application.
        /// </summary>
        /// <returns></returns>
        // <RunDatabaseDemo>
        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
            Database database = databaseResponse;

            // The response from Azure Cosmos
            DatabaseProperties properties = databaseResponse;

            Console.WriteLine($"\n1. Create a database resource with id: {properties.Id} and last modified time stamp: {properties.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
            ThroughputResponse throughputResponse = await database.ReadThroughputAsync();

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

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

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

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

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

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