Azure cosmosdb 创建删除database,container和增删改查操作

本文代码摘自cosmosdb官网,然后仿照着自己添加了一个删除container的函数,记录在此以便查阅。

想自行尝试的话可以去Home - Microsoft Azure官网按照教程操作一遍,会自动下载一个项目到本地,然后自己跑跑就好了:

 其实核心思路就是:

  1. 首先创建一个用于连接azure cosmosdb的cosmosclient实例
  2. 然后就利用cosmosclient上提供的API进行各种操作

常用的操作包含下如下几个函数里,可以自行查看:

CreateDatabaseAsync();  // 创建database
CreateContainerAsync();  // 创建container,可以理解为创建表
ScaleContainerAsync();  // 设置吞吐量
AddItemsToContainerAsync();  // 插入一条记录
QueryItemsAsync();   // 查询一条记录
ReplaceFamilyItemAsync();  // 修改
DeleteFamilyItemAsync();  // 删除
DeleteContainerAndCleanupAsync();  // 删除container
DeleteDatabaseAndCleanupAsync();  // 删除数据库

完整代码: 

using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos;

namespace CosmosGettingStartedTutorial
{
    class Program
    {
        // The Azure Cosmos DB endpoint for running this sample.
        private static readonly string EndpointUri = ConfigurationManager.AppSettings["EndPointUri"];

        // The primary key for the Azure Cosmos account.
        private static readonly string PrimaryKey = ConfigurationManager.AppSettings["PrimaryKey"];

        // The Cosmos client instance
        private CosmosClient cosmosClient;

        // The database we will create
        private Database database;

        // The container we will create.
        private Container container;

        // The name of the database and container we will create
        private string databaseId = "ToDoList";
        private string containerId = "Items";

        // <Main>
        public static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("Beginning operations...\n");
                Program p = new Program();
                await p.GetStartedDemoAsync();

            }
            catch (CosmosException de)
            {
                Exception baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
            finally
            {
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
        // </Main>

        // <GetStartedDemoAsync>
        /// <summary>
        /// Entry point to call methods that operate on Azure Cosmos DB resources in this sample
        /// </summary>
        public async Task GetStartedDemoAsync()
        {
            // Create a new instance of the Cosmos Client
            this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey, new CosmosClientOptions() { ApplicationName = &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值