Register Default Data Connections
- 2 minutes to read
This document describes how to provide a Web Dashboard with a set of data connections. These connections are available in the Dashboard Data Source Wizard when users create new data sources.
To create a data connection, add a connection string to the appsetings.json file or specify a data connection provider:
Add a Connection String to appsettings.json
A connection string should contain the XpoProvider parameter that depends on the database type:
{
"ConnectionStrings": {
"MS SQL Connection": "XpoProvider=MSSqlServer; data source=localhost; initial catalog=Northwind; Integrated Security=SSPI; Persist Security Info=True;",
"XML Connection": "XpoProvider=InMemoryDataStore;Read Only=true;Data Source=Data\\nwind.xml;",
"JSON Connection to URI": "uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"
}
}
See Custom Connection Strings for Data Sources for information on how to specify a custom connection string.
To allow users to create new data sources based on available connection strings, create the DashboardConnectionStringsProvider instance and pass it to the DashboardConfigurator.SetConnectionStringsProvider method:
using DevExpress.DashboardWeb;
// ...
public void ConfigureServices(IServiceCollection services) {
services
// ...
.AddDefaultDashboardController(configurator => {
configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(Configuration));
});
// ...
}
Specify a Data Connection Provider
You can use the DashboardConfigurator.SetConnectionStringsProvider method to specify a data connection provider.
This example shows how to implement a custom connection strings provider by implementing the IDataSourceWizardConnectionStringsProvider interface:
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Native;
using DevExpress.DataAccess.Web;
// ...
public class MyDataSourceWizardConnectionStringsProvider : IDataSourceWizardConnectionStringsProvider {
public Dictionary<string, string> GetConnectionDescriptions() {
Dictionary<string, string> connections = new Dictionary<string, string>();
// Customize the loaded connections list.
connections.Remove("LocalSqlServer");
connections.Add("msSqlConnection", "MS SQL Connection");
connections.Add("xmlConnection", "XML Connection");
return connections;
}
public DataConnectionParametersBase GetDataConnectionParameters(string name) {
// Return custom connection parameters for the custom connection.
if (name == "msSqlConnection") {
return new MsSqlConnectionParameters("localhost", "Northwind", "", "", MsSqlAuthorizationType.Windows);
} else if (name == "xmlConnection") {
return new XmlFileConnectionParameters("C:\\Users\\Public\\Documents\\DevExpress Demos 18.2\\Components\\Data\\nwind.xml");
}
return AppConfigHelper.LoadConnectionParameters(name);
}
}
After implementing a custom connection strings provider, pass this class’s instance to the DashboardConfigurator.SetConnectionStringsProvider method:
using DevExpress.DashboardWeb;
// ...
public void ConfigureServices(IServiceCollection services) {
services
// ...
.AddDefaultDashboardController(configurator => {
configurator.SetConnectionStringsProvider(new MyDataSourceWizardConnectionStringsProvider());
});
// ...
}