Microsoft SQL Data Binding in Blazor Pivot Table
5 Nov 202512 minutes to read
This guide explains how to connect a Microsoft SQL Server database to the Blazor Pivot Table using the System.Data.SqlClient library. It covers two methods: directly retrieving and binding data to the Pivot Table and using a Web API service to fetch and display Microsoft SQL data.
Connecting a Microsoft SQL database to a Syncfusion® Blazor Pivot Table
This section describes how to connect the Blazor Pivot Table to a Microsoft SQL database by directly retrieving data using the System.Data.SqlClient library.
Step 1: Set up a Blazor Pivot Table
- Create a Blazor Pivot Table by following the Getting Started guide.
Step 2: Install the System.Data.SqlClient NuGet package
- Open the NuGet Package Manager in your project solution.
- Search for and install the System.Data.SqlClient package to enable Microsoft SQL Server connectivity.

Step 3: Connect to the Microsoft SQL database
In the Index.razor file, under the OnInitialized method, use the System.Data.SqlClient library to connect to a Microsoft SQL database and retrieve data for the Pivot Table.
-
Establish connection: Use SqlConnection with a valid connection string, such as
Server=localhost;Database=Database1;Trusted_Connection=True;, to connect to the Microsoft SQL database. -
Query and fetch data: Execute a SQL query, such as
SELECT * FROM table1, using SqlCommand to retrieve data for the Pivot Table. - Structure the data: Use the Fill method of SqlDataAdapter to populate the query results into a DataTable, which is then converted to a list for binding to the Pivot Table.
Step 4: Bind data to the Pivot Table
- Assign the retrieved list to the DataSource property of the PivotViewDataSourceSettings.
- Configure the Pivot Table by defining fields in the PivotViewColumns, PivotViewRows, PivotViewValues, and PivotViewFormatSettings to organize and format the data.
@using Syncfusion.Blazor.PivotView;
@using System.Data;
@using System.Data.SqlClient;
<SfPivotView TValue="OrderDetails" Width="800" Height="360">
<PivotViewDataSourceSettings TValue="OrderDetails" DataSource="@dataSource">
<PivotViewColumns>
<PivotViewColumn Name="Product"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Date"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C2"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code {
private List<OrderDetails> dataSource { get; set; }
protected override void OnInitialized()
{
string conSTR = @"<Enter a valid connection string>";
string xquery = "SELECT * FROM table1";
SqlConnection sqlConnection = new(conSTR);
sqlConnection.Open();
SqlCommand sqlCommand = new(xquery, sqlConnection);
SqlDataAdapter dataAdapter = new(sqlCommand);
DataTable dataTable = new();
dataAdapter.Fill(dataTable);
dataSource = (from DataRow data in dataTable.Rows
select new OrderDetails()
{
Quantity = Convert.ToInt32(data["Quantity"]),
Product= data["Product"].ToString(),
Date = data["Date"].ToString(),
Country = data["Country"].ToString(),
Amount = Convert.ToDouble(data["Amount"])
}).ToList();
}
public class OrderDetails
{
public int Quantity { get; set; }
public string Product { get; set; }
public string Date { get; set; }
public string Country { get; set; }
public double Amount { get; set; }
}
}Step 5: Run and verify the Pivot Table
- Run the Blazor application.
- The Pivot Table will display the Microsoft SQL data, organized according to the defined configuration.
- The resulting Pivot Table will look like this:

Connecting a Microsoft SQL to a Syncfusion® Blazor Pivot Table via Web API service
This section explains how to create a Web API service to fetch data from a Microsoft SQL database and connect it to the Blazor Pivot Table.
Create a Web API service to fetch Microsoft SQL data
Follow these steps to set up a Web API service that retrieves Microsoft SQL data for the Pivot Table.
Step 1: Create an ASP.NET Core Web application
- Open Visual Studio and create a new ASP.NET Core Web App project named MyWebService.
- Refer to the Microsoft documentation for detailed setup instructions.

Step 2: Install the System.Data.SqlClient NuGet package
- Open the NuGet Package Manager in your project solution.
- Search for and install the System.Data.SqlClient package to enable Microsoft SQL Server connectivity.

Step 3: Create a Web API controller
- In the Controllers folder, create a new Web API controller named PivotController.cs.
- This controller manages data communication between the Microsoft SQL database and the Pivot Table.
Step 4: Connect to Microsoft SQL and retrieve data
In the PivotController.cs file, use the System.Data.SqlClient library to connect to a Microsoft SQL database and fetch data for the Pivot Table.
- Establish connection: Use SqlConnection with a valid connection string to access the Microsoft SQL database.
-
Fetch data: Run a SQL query, such as
SELECT * FROM table1, using SqlCommand to retrieve data. - Prepare data: Use the Fill method of SqlDataAdapter to store the query results in a DataTable for JSON serialization.
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Data;
using System.Data.SqlClient;
namespace MyWebService.Controllers
{
[ApiController]
[Route("[controller]")]
public class PivotController : ControllerBase
{
private static DataTable FetchSQLResult()
{
string conSTR = @"<Enter a valid connection string>";
string xquery = "SELECT * FROM table1";
SqlConnection sqlConnection = new(conSTR);
sqlConnection.Open();
SqlCommand sqlCommand = new(xquery, sqlConnection);
SqlDataAdapter dataAdapter = new(sqlCommand);
DataTable dataTable = new();
dataAdapter.Fill(dataTable);
sqlConnection.Close();
return dataTable;
}
}
}Step 5: Serialize data to JSON
- In the PivotController.cs file, create a Get method that calls FetchSQLResult to retrieve Microsoft SQL data.
- Use JsonConvert.SerializeObject from the Newtonsoft.Json library to serialize the DataTable into JSON format.
Ensure the Newtonsoft.Json NuGet package is installed in your project.
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Data;
using System.Data.SqlClient;
namespace MyWebService.Controllers
{
[ApiController]
[Route("[controller]")]
public class PivotController : ControllerBase
{
[HttpGet(Name = "GetSQLResult")]
public object Get()
{
return JsonConvert.SerializeObject(FetchSQLResult());
}
private static DataTable FetchSQLResult()
{
string conSTR = @"<Enter a valid connection string>";
string xquery = "SELECT * FROM table1";
SqlConnection sqlConnection = new(conSTR);
sqlConnection.Open();
SqlCommand sqlCommand = new(xquery, sqlConnection);
SqlDataAdapter dataAdapter = new(sqlCommand);
DataTable dataTable = new();
dataAdapter.Fill(dataTable);
sqlConnection.Close();
return dataTable;
}
}
}Step 6: Run the Web API service
- Build and run the application.
- The application will be hosted at
https://localhost:7139/(the port number may vary).
Step 7: Verify the JSON data
- Access the Web API endpoint at
https://localhost:7139/Pivotto view the JSON data retrieved from the Microsoft SQL database. - The browser will display the JSON data, as shown below.

Connecting the Pivot Table to a Microsoft SQL database using the Web API service
This section explains how to connect the Blazor Pivot Table to Microsoft SQL data retrieved via the Web API service.
Step 1: Set up a Blazor Pivot Table
- Create a Blazor Pivot Table by following the Getting Started guide.
Step 2: Configure the Web API URL
- In the Index.razor file, map the Web API URL (
https://localhost:7139/Pivot) to the Pivot Table using the Url property of PivotViewDataSourceSettings. - The Url property deserialize Microsoft SQL data into instances of your model data class (e.g.,
TValue="OrderDetails") for binding to the Pivot Table.
Step 3: Define the Pivot Table report
- Configure the Pivot Table by defining fields in the PivotViewColumns, PivotViewRows, PivotViewValues, and PivotViewFormatSettings properties.
- Enable the field list by setting ShowFieldList to true for interactive field management.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="OrderDetails" Width="1000" Height="300" ShowFieldList="true">
<PivotViewDataSourceSettings TValue="OrderDetails" Url="https://localhost:7139/Pivot" ExpandAll=false EnableSorting=true>
<PivotViewColumns>
<PivotViewColumn Name="Product"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Date"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C2"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
<PivotViewGridSettings ColumnWidth="120"></PivotViewGridSettings>
</SfPivotView>
@code {
public class OrderDetails
{
public int Quantity { get; set; }
public string Product { get; set; }
public string Date { get; set; }
public string Country { get; set; }
public double Amount { get; set; }
}
}Step 4: Run and verify the Pivot Table
- Run the Blazor application.
- The Pivot Table will display the Microsoft SQL data fetched via the Web API, structured according to the defined configuration.
- The resulting Pivot Table will look like this:

Additional resources
Explore a complete example of the Blazor Pivot Table integrated with an ASP.NET Core Web application to fetch data from a Microsoft SQL database in this GitHub repository.