Custom Connection String
- 4 minutes to read
The Dashboard Designer allows you to connect to different types of SQL databases in the Data Source Wizard. You can also use data access API to connect to the database and select data in code.
This tutorial describes how to create a custom connection string that allows you to pass additional connection parameters and establish a connection to any database.
Important
The appropriate data provider must be installed on the client machine. For information on data providers, refer to the Data Sources article.
Create a Data Source in the Data Source Wizard
To connect to a database in the Dashboard Designer, follow the steps below:
Click the New Data Source button in the Data Source ribbon tab.
On the first page of the invoked Data Source Wizard dialog, select Custom Connection String and click Next.
On the next page, specify the Connection string.
Note
To specify the data provider for the custom connection string, use the XpoProvider parameter. Refer to the How to Specify a Custom Connection String for an SQL Data Source topic to see how to specify this parameter.
Click Next and specify how to select data from the database.
You can choose from the following options:
Select the Query option and click Run Query Builder… to invoke the Query Builder. The Query Builder allows you to choose the tables/columns and passes the resulting SQL query to the SQL String editor. Subsequently, the text of the generated query returns to the Data Source wizard.
Click Finish to create the data source.
Note
If you apply filtering or use query parameters, click Next to specify the parameter settings.
Select the Stored Procedure option to select one of the stored procedures from the database.
Then click Next to specify the parameter settings.
On the final page, you can optionally add query parameters and preview data.
Click Finish to create the data source.
Create a Data Source in Code
To create a data source that uses a custom connection string to connect to a database, create an instance of the DashboardSqlDataSource class and follow the steps below:
Create the CustomStringConnectionParameters class object and specify the CustomStringConnectionParameters.ConnectionString property.
Assign the resulting CustomStringConnectionParameters object to the SqlDataSource.ConnectionParameters property.
Note
Alternatively, you can add a connection string with parameters to the application configuration file. Then, assign the connection string name to the SqlDataSource.ConnectionName property.
Create one of the following objects to specify the query:
- Create a SelectQuery object to specify a set of tables/columns that form a SELECT statement when you execute a query.
- Create a CustomSqlQuery object to specify an SQL query. Use the CustomSqlQuery.Sql property to specify a custom query string.
- Create a StoredProcQuery object to execute a stored procedure call and supply the dashboard with data.
Add the created query to the SqlDataSource.Queries collection exposed by the DashboardSqlDataSource object.
- Add the created DashboardSqlDataSource object to the Dashboard.DataSources collection.
The following code snippet shows how to supply the dashboard with data from the Northwind database deployed on the Microsoft SQL Server by specifying a connection string:
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
// ...
CustomStringConnectionParameters customstringParams = new CustomStringConnectionParameters();
customstringParams.ConnectionString = "XpoProvider=MSSqlServer; Data Source=localhost; Initial Catalog=Northwind; User Id=username; Password=password;";
DashboardSqlDataSource sqlDataSource = new DashboardSqlDataSource("Data Source 1", customstringParams);
SelectQuery selectQuery = SelectQueryFluentBuilder
.AddTable("SalesPerson")
.SelectColumns("CategoryName", "Extended Price")
.Build("Query 1");
sqlDataSource.Queries.Add(selectQuery);
sqlDataSource.Fill();
dashboard.DataSources.Add(sqlDataSource);