You are correct. A best practice is to keep the connection string out of the client to avoid security exploits. Generally, there are two scenarios where multiple databases are needed. These are deploying to multiple environments and multi-tenancy. Let me go over these below.
Deploying to Multiple Environments
The provided solution in the original post is for switching on build configurations that are deployed to different environments. ASP.NET has several mechanisms for handling different build configurations out-of-the-box. For example, see Config Transforms in ASP.NET Core and web.config transforms in ASP.NET Full Framework.
In Telerik Reporting, when using the proper configuration transformation method, a Shared Connection String will work without any additional code.
This works because when creating a DataSource Component with shared connection string, the ConnectionString property is set to the name of the shared connection in the Report Designer. See the following screenshot for a reference.
When using the above report in a .NET application, Telerik Reporting will match this ConnectionString property to the Connection String name provided in the application configuration. See the following app.config for an example.
Since the configuration transforms the connectionString value per environment, multiple database connections can be used.
connectionString="Data Source=(localdb)\mssqllocaldb;Initial Catalog=AdventureWorks;Integrated Security=SSPI"
An example of this is provided in the default installation folder. The two configurations to reference are listed below.
- The ReportLibrary app.config: C:\Program Files (x86)\Progress\[Telerik Reporting Release Version]\Examples\CSharp\ReportLibrary\app.config
- The MVC Demo Web.config: C:\Program Files (x86)\Progress\[Telerik Reporting Release Version]\Examples\CSharp\MvcDemo\Web.config
- NOTE: The build configs are named Web.Debug.config and Web.Release.config. If deploying to Release a new connection string could be used pointing to a different server and database.
Multi-tenancy is far more complex and requires careful planning. Ultimately, Telerik Reporting and multi-tenancy are unrelated which is primarily why we don't have a demo for it. It's technically not part of the Telerik Support options.
However, once multi-tenancy is set up, the TenantID can be used as a Report Parameter allowing preparation of the data accordingly.
Another example that I thought of as I was writing this is when a report parameter is used as a binding expression. In this case, the connection string could be generated dynamically and bound to a Data Item without having the connection string in the application or the report.
I hope this provides a better understanding of why we recommend using a Report Parameter for dynamic connection strings.
Please let me know if you need any additional information. Thank you for using the Reporting Forums.
Eric R | Senior Technical Support Engineer
Progress is here for your business, like always. Read more
about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.