Connect to Cosmos DB Data from Blazor Apps



Build ASP.NET Core Blazor C# apps that integrate with real-time Cosmos DB data using standard SQL.

Blazor is a framework for developing modern, client-side web UIs using .NET technology. Instead of coding in JavaScript, developers can use the familiar C# language and .NET libraries to build app UIs.

The CData ADO.NET Provider for Cosmos DB can be used with standard ADO.NET interfaces, such as LINQ and Entity Framework, to interact with live Cosmos DB data. Since Blazor supports .NET Core, developers can use CData ADO.NET Providers in Blazor apps. In this article, we will guide you to build a simple Blazor app that talks to Cosmos DB using standard SQL queries.

Install the CData ADO.NET Provider for Cosmos DB

CData ADO.NET Providers allow users to access Cosmos DB just like they would access SQL Server, using simple SQL queries.

Install the Cosmos DB ADO.NET Data Provider from the CData website or from NuGet. Search NuGet for "Cosmos DB ADO.NET Data Provider."

Create a Cosmos DB-Connected Blazor App

Start by creating a Blazor project that references the CData ADO.NET Provider for Cosmos DB

  1. Create a Blazor project on Visual Studio.
  2. From the Solution Explorer, right click Dependencies, then click Add Project Reference.
  3. In the Reference Manager, click the Browse button, and choose the .dll file of the installed ADO.NET Provider (e.g. System.Data.CData.CosmosDB.dll, typically located at C:\Program Files\CData\CData ADO.NET Provider for Cosmos DB\lib etstandard2.0).

SELECT Cosmos DB Data from the Blazor App

  1. Open the Index.razor file from the Project page.
  2. In a CosmosDBConnection object, set the connection string:

    To obtain the connection string needed to connect to a Cosmos DB account using the SQL API, log in to the Azure Portal, select Azure Cosmos DB, and select your account. In the Settings section, click Connection String and set the following values:

    • AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account
    • AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. From the resource menu, go to the Keys page. Find the PRIMARY KEY value and set AccountKey to this value.

    For example: AccountEndpoint=myAccountEndpoint;AccountKey=myAccountKey;

  3. The code below creates a simple Blazor app for displaying Cosmos DB data, using standard SQL to query Cosmos DB just like SQL Server.

          @page "/"
          @using System.Data;
          @using System.Data.CData.CosmosDB;
          
          <h1>Hello, world!</h1>
          
          Welcome to your Data app.
          
          <div class="row">
              <div class="col-12">
          
                  @using (CosmosDBConnection connection = new CosmosDBConnection(
                    "AccountEndpoint=myAccountEndpoint;AccountKey=myAccountKey;"))
                  {
                      var sql = "SELECT City, CompanyName FROM Customers";
                      var results = new DataTable();
          
                      CosmosDBDataAdapter dataAdapter = new CosmosDBDataAdapter(sql, connection);
                      dataAdapter.Fill(results);
          
                      <table class="table table-bordered">
                          <thead class="thead-light">
                              <tr>
                                  @foreach (DataColumn item in results.Rows[0].Table.Columns)
                                  {
                                      <th scope="col">@item.ColumnName</th>
                                  }
                              </tr>
                          </thead>
                          <tbody>
                              @foreach (DataRow row in results.Rows)
                              {
                                  <tr>
                                      @foreach (var column in row.ItemArray)
                                      {
                                          <td>@column.ToString()</td>
                                      }
                                  </tr>
                              }
                          </tbody>
                      </table>
                  }
              </div>
          </div>
        
  4. Rebuild and run the project. The ADO.NET Provider renders Cosmos DB data as an HTML table in the Blazor app.

    At this point, you have a Cosmos DB-connected Blazor app, capable of working with live Cosmos DB data just like you would work with a SQL Server instance. Download a free, 30-day trial and start working with live Cosmos DB data in your Blazor apps today.

Ready to get started?

Download a free trial of the Cosmos DB Data Provider to get started:

 Download Now

Learn more:

Cosmos DB Icon Cosmos DB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Cosmos DB document databases.