Skip to main content
A newer version of this page is available. .

Connect the ASP.NET Core Dashboard Control to an SQL Database

  • 2 minutes to read

This tutorial shows how to add the DashboardSqlDataSource to data source storage and make it available to users. The sample SQLite database supplies the dashboard with data.

  1. In your application, create the Data folder and add the nwind.db database to it from the C:\Users\Public\Documents\DevExpress Demos 19.2\Components\Data folder.

  2. In the Startup.cs file, create a public method that returns the configured dashboard’s data source storage (DataSourceInMemoryStorage).

    using DevExpress.DataAccess.Sql;
    
    public DataSourceInMemoryStorage CreateDataSourceStorage() {
        DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
    
               DashboardSqlDataSource sqlDataSource = new DashboardSqlDataSource("SQL Data Source", "sqliteConnection");
                sqlDataSource.DataProcessingMode = DataProcessingMode.Client;
                DevExpress.DataAccess.Sql.SelectQuery query = DevExpress.DataAccess.Sql.SelectQueryFluentBuilder
                    .AddTable("SalesPerson")
                    .SelectAllColumns()
                    .Build("Sales Person");
                sqlDataSource.Queries.Add(query);
                dataSourceStorage.RegisterDataSource("sqlDataSource", sqlDataSource.SaveToXml());         
    
        return dataSourceStorage;
    }
    
  3. Call the DashboardConfigurator.SetDataSourceStorage method to configure the data source storage. Use the created CreateDataSourceStorage method as the SetDataSourceStorage parameter. Then subscribe to the DashboardConfigurator.ConfigureDataConnection event to pass the connection parameters the SQL data source requires.

    using DevExpress.AspNetCore;
    using DevExpress.DashboardAspNetCore;
    using DevExpress.DashboardWeb;
    
    public void ConfigureServices(IServiceCollection services) {            
        services
            .AddMvc()
            .AddDefaultDashboardController(configurator => {
                // ...
                configurator.SetDataSourceStorage(CreateDataSourceStorage());
                configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
            });
    }
    
  4. Provide the connection parameters at runtime in the DashboardConfigurator.ConfigureDataConnection event handler.

    using DevExpress.DataAccess.ConnectionParameters;
    
    private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
        if (e.ConnectionName == "sqliteConnection") {
            SQLiteConnectionParameters sqliteParams = new SQLiteConnectionParameters();
            sqliteParams.FileName = "file:Data/nwind.db";
            e.ConnectionParameters = sqliteParams;
        }
    }
    
  5. As a result, the SQL Data Source is displayed as a Web Dashboard’s predefined data source.

web-dashboard-ex-sql-data-source

Users can bind dashboard items to data in the Web Dashboard’s UI. See Bind Dashboard Items to Data in the Web Dashboard’s UI for more information.

Example

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

Note

A complete sample project is available on GitHub: How to Register Data Sources for ASP.NET Core Dashboard Control