Dynamic Run time Connection String

Posts   
 
    
hotmail
User
Posts: 47
Joined: 06-Feb-2013
# Posted on: 14-Apr-2020 22:31:29   

Hello

Using .NET Core, SQL Server 2019.

I have a SQL server with multiple databases for different customers. the database names example are

-ABC -XYZ -ABR

My product is a .NET Core Angular application. I want to have one application running but be able to connect to each individual database based on some pattern or file.

For example

www.product.com/ABC > Points to ABC db www.product.com/XYZ > Points to XYZ db www.product.com/ABR > Points to ABR db

So the connection string has to be dynamic for each call during run-time. V5.6 documentation suggests setting a connection string during startup.

Can you please help me with the exact setup and steps I have to take to accomplish this.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Apr-2020 05:57:07   

Hi there.

As you mentioned, at startup you should configure your default settings and connection strings, as mentioned here.

Now, what you really want is change the database (catalog) names at runtime dynamically. To do so, you should use Multi-tenancy support: catalog specific persistence info. Then you could add an overwrite for each instance of DataAccessAdapter...:

var customerDBName = ObtainTheCatalogNameInstance(); 

// initializing the overwrite table. Here XXX represents the generic catalog/database name used in your LLBLGen project.
var overwrites = new CatalogNameOverwriteHashtable();
overwrites.Add("XXX", customerDBName);

// ... later in the code I pass it to an adapter instance for usage
// in the query
using(var adapter = new DataAccessAdapter())
{
    adapter.CatalogNameOverwrites = overwrites;
    
    //... further query code.
}

Hope that helps wink

David Elizondo | LLBLGen Support Team