Changing your connection string at runtime with Spring.Net

Something that pops up every once in a while on the Spring.Net user forum is how to change your connection string at runtime. You need this behaviour for instance when you have a multi tenant application.

Out of the box Spring.Net comes with two DbProviders to change information about your connection string at runtime. One provider to change the credentials at runtime named UserCredentialsDbProvider, and one that can choose a connectionstring from a predefined list called MultiDelegatingDbProvider.

If you want to change the connection string at runtime, for instance based on the current logged in user, what works best is to either subclass DelegatingDbProvider or UserCredentialsDbProvider (which actually is a subclass of DelegatingDbProvider).

Attached you can find a quick and dirty sample which shows what you need to hook up.

RuntimeConnectionString.zip (8.29 kb)

One Reply to “Changing your connection string at runtime with Spring.Net”

  1. Thanks for this.. Found link picking through forums at springframework.net. Your sample got me going. This seems like such a pervasive need that it would be baked directly into framework. Anyway, thanks again.

    Joel

Leave a Reply

Your email address will not be published. Required fields are marked *