Back to Devexpress

SQL Data Source in ASP.NET Core

dashboard-116652-web-dashboard-integrate-dashboard-component-dashboard-backend-prepare-data-source-storage-for-the-aspnet-core-framework-sql-data-source.md

latest6.4 KB
Original Source

SQL Data Source in ASP.NET Core

  • Feb 27, 2023
  • 3 minutes to read

This topic shows how to add the DashboardSqlDataSource to an in-memory data source storage, and make it available to users.

To configure an SQL data source, specify a database connection first and then create the data source in code or in the UI.

Specify a Database Connection

You can specify a connection to the database in one of the following ways:

Create an SQL Data Source in Code

Create a Data Source

To create a new SQL Data Source, follow the steps below:

  1. Create a DashboardSqlDataSource instance.
  2. Assign the data connection name you specified earlier to the SqlDataSource.ConnectionName property.
  3. Create the Select query. Use one of the following objects:
  • The SelectQueryFluentBuilder object specifies a set of tables/columns that form a SELECT statement when you execute a query.
  • The CustomSqlQuery object specifies an SQL query manually as a query string.
  • The StoredProcQuery object performs a stored procedure call to supply the dashboard with data.
  1. Add the query object to the SqlDataSource.Queries collection.
cs
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.Sql;

DashboardSqlDataSource sqlDataSource = new DashboardSqlDataSource("SQL Data Source", "NWindConnectionString");
sqlDataSource.DataProcessingMode = DataProcessingMode.Client;
SelectQuery query = SelectQueryFluentBuilder
    .AddTable("Categories")
    .Join("Products", "CategoryID")
    .SelectAllColumnsFromTable()
    .Build("Products_Categories");
sqlDataSource.Queries.Add(query);

Register the Data Source in the Storage

Call the DataSourceInMemoryStorage.RegisterDataSource method to register the data source in the data source storage. Call the DashboardConfigurator.SetDataSourceStorage method to specify the data source storage for the Web Dashboard.

cs
// Create a data source storage.
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

// Register the SQL data source.
dataSourceStorage.RegisterDataSource("sqlDataSource", sqlDataSource.SaveToXml());

// Register the storage for the Web Dashboard.
configurator.SetDataSourceStorage(dataSourceStorage);

The SQL Data Source is now available in the Web Dashboard:

Users can bind dashboard items to data in the Web Dashboard’s UI.

Create an SQL Data Source in the UI

Users can use the Dashboard Data Source Wizard to create a new SQL data source based on an existing connection.

They can create/edit a query, select a stored procedure, or add query parameters.

See the following topic for details: Specify Data Source Settings (Database).

Example

The example shows how to make a set of data sources available for users in the Web Dashboard application.

View Example: How to Register Data Sources for ASP.NET Core Dashboard Control