corelibraries-403635-devexpress-data-library-data-sources-use-the-sql-data-source-manage-connection-at-runtime.md
Use the SqlDataSourceUIHelper.ConfigureConnection and SqlDataSourceUIHelper.ConfigureConnectionParameters methods to invoke the Connection Editor wizard at runtime. The ConfigureConnection and ConfigureConnectionParameters methods are available as the SqlDataSource class extension methods if the project references the DevExpress.DataAccess.v25.2.UI.dll and the DevExpress.DataAccess.UI.Sql namespace is explicitly imported with a using directive:
using DevExpress.DataAccess.UI.Sql;
You can specify the ConfigureConnectionContext instance as the method parameter to customize the Connection Editor.
The following code customizes the list of available data providers. It removes all data providers except the MSSqlServer.
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.UI.Sql;
SqlDataSource sqlDS = new SqlDataSource();
ConfigureConnectionContext config = new ConfigureConnectionContext();
config.DataProviders.RemoveAll(x => x.ProviderKey != "MSSqlServer");
sqlDS.ConfigureConnectionParameters(config);
Imports DevExpress.DataAccess.Sql
Imports DevExpress.DataAccess.UI.Sql
Dim sqlDS As SqlDataSource = New SqlDataSource()
Dim config As ConfigureConnectionContext = New ConfigureConnectionContext()
config.DataProviders.RemoveAll(Function(x) x.ProviderKey <> "MSSqlServer")
sqlDS.ConfigureConnectionParameters(config)
The following code restricts the user to select a connection from the list.
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.UI.Sql;
SqlDataSource sqlDS = new SqlDataSource();
ConfigureConnectionContext config = new ConfigureConnectionContext();
config.Options = DevExpress.DataAccess.Wizard.SqlWizardOptions.DisableNewConnections;
sqlDataSource1.ConfigureConnection(config);
Imports DevExpress.DataAccess.Sql
Imports DevExpress.DataAccess.UI.Sql
Dim sqlDS As SqlDataSource = New SqlDataSource()
Dim config As ConfigureConnectionContext = New ConfigureConnectionContext()
config.Options = DevExpress.DataAccess.Wizard.SqlWizardOptions.DisableNewConnections
sqlDS.ConfigureConnection(config)
You can define different connection strings in the application configuration file and use the connection name to specify the SqlDataSource.ConnectionName property when the SqlDataSource is initialized.
The SqlDataSource.ConfigureDataConnection event occurs if the SqlDataSource.ConnectionParameters property is specified. The event is not raised if the data connection is stored by name, without parameters. The event is raised each time the SqlDataSource connects to a database. You can handle this event to change the parameters required to establish a data connection (server name, database name, user credentials, data file path).
The ConfigureDataConnection event handler allows you to use the e.ConnectionName property to identify the name of the current connection and e.ConnectionParameters property to access and modify connection parameters. You cannot modify the SqlDataSource.ConnectionName property in the event handler.
The DataSourceDemanded event occurs before a report generates a document, and allows you to specify the report’s data source.
If the application loads a report from a .REPX file (deserializes the report), you can use a custom service to resolve the connection name stored in the report layout file. Add a custom service that implements the IConnectionProviderService interface to the report instance (the report is cast to the IServiceContainer interface). The report calls the IConnectionProviderService.LoadConnection method for each data connection stored in a REPX file, if the connection is stored by name, without connection parameters.
Handle the Dashboard.ConfigureDataConnection event that occurs before the dashboard populates its data source.