Skip to content
Related Articles

Related Articles

Improve Article

Microsoft Azure – Using C# in CosmoDB

  • Last Updated : 01 Jun, 2021

In this article, we will look into how to use C# notebooks in Azure Cosmos DB. When you are creating an application that uses Azure Cosmos DB, it is useful to experiment with code that uses that Cosmos DB. In Azure Cosmos DB, you can use C# notebooks that enable you to write the C# code that interacts with Cosmos DB. 

To do so follow the below steps:

Step 1: In the Azure portal we have an already existing Cosmos DB. We’ve populated it with data by using the Start with Sample button.

Step 2: To use notebooks, we first need to enable them and complete the setup. 



This creates a notebook workspace in the Cosmos DB. 

Step 3: Now let’s try it by creating a new notebook by clicking on the New Notebook menu. 

This is the new notebook. It has a new cell that can run code. You can also create cells that display text. 

Step 4: The sample data contains Person data, and the below image is a representation of that. 



The above code is of C# and we can run it with the below-highlighted button. 

Step 5: Make sure that the language setting is set to CSharp. 

Step 6: Now let’s add another code cell, and here we paste the below code. This code connects to the Cosmos DB and executes a query against it using the Cosmos DB .NET SDK.

C#




using System.Linq.Expressions;
  
// namespace for Azure Cosmos DB .NET V3 SOK
using Microsoft.Azure.Cosmos;
using System.Collections;
  
// Initialize a new instance of cosmosClient using 
// the built-in account endpoint and key parameters 
Cosmosclient cosmosClient = new CosmosClient (Cosmos.Endpoint, Cosmos.Key);
Microsoft.Azure.Cosmos.Database database = await 
cosmosClient.CreateDatabaseIfNotExistsAsync("SampleDB");
Container container = await database.CreateContainerIfNotExistsAsync("Persons",
                                                                     "/firstname",
                                                                     400);
  
QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM c");
  
FeedIterator<Person> queryResultSetIterator = container.GetItemQueryIterator<Person>(queryDefinition);
  
List<Person> personEvents = new List<Person();
  
while (queryResultSetIterator.HasMoreResults)
{
  
FeedResponse<Person> currentResultSet = await queryResultSetIterator.ReadNextAsync();
foreach (Person personEvent in currentResultSet)
{
personEvents.Add(personEvent);
}
}
  
personEvents

It then puts the query results in a list of person objects and outputs by simply adding it to the last line of code. It also triggers the notebook to start the data visualization feature. Also, note that we don’t have to insert the actual endpoint and key. These are already known by the system. 

Step 7: Now let’s run this. This results in a table with the data. 

We can also switch to other views of the data. The workspace also contains a gallery. This gallery contains example notebooks that you can browse and download to your workspace to play with and learn from.

The Azure Cosmos DB .NET SDK enables you to manage your Azure Cosmos DB and interact with its data. You can use the SDK in intelligent notebooks within Azure Cosmos DB that enable you to run code at descriptive texts and visualize data. 




My Personal Notes arrow_drop_up
Recommended Articles
Page :