Change connection string at runtime! Is it effect on your application?

Change connection string at runtime! Is it effect on your application?

Old forum URL: forums.lhotka.net/forums/t/6407.aspx


Raghav posted on Thursday, February 12, 2009

Normal 0 false false false EN-US X-NONE X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

Dear friends

I am using CSLA.NET 3.6 version. I have some issue with connection string.

See my connection string is as mentioned below (before running my application),

Connection string file is MyApp.exe.config

<add name="MyApp" connectionString="Data Source=;Initial Catalog=;User ID=;Password=" providerName="System.Data.SqlClient" />

While logging in I change my connection sting as mentioned below,

<add name="MyApp" connectionString="Data Source=MyServer;Initial Catalog=MyDB;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />

I have changed highlighted words in my connection string file while logging in.

The problem is that application will not take the modified connection string while logging in, but when I run my application next time it takes the modified connection string.

Application takes a connection string from MyApp.exe.config file while loading application. This connection string remains same until unless we close the application.  That means connection string loads only once at a time while loading an application.

We can change connection string in config file while application running, but It will not effect on application.

I want to change my connection string at runtime and it also should effect on application, please help me out!

Thanks

Raghav

JoOfMetL replied on Thursday, February 12, 2009


You can try to refresh your connectionString Section :

ConfigurationManager.RefreshSection("connectionStrings");

rsbaker0 replied on Thursday, February 12, 2009

Note that, depending on your application architecture, changing the connection may have other ramifications.

In particular, if you have statically loaded, cached, or lazily loaded data that is specific to the connection (e.g. NameValueLists for example) and these aren't the same from one database to another, you may to implement some pretty exciting plumbing to ensure they are reset.

In our case, the per-class business rules even change from one database to another, so we are basically forced to exit and respawn the application if you change the connection.

Raghav replied on Friday, February 13, 2009

its working, great help! thanks a lot!!!!!!!!! Raghav

Fintanv replied on Thursday, February 12, 2009

I have also done this in the past to allow switching from production to test (qa) to dev environments.  You just need some mechanism to re-load the correct string and ensure your DAL will be using it for all subsequent database calls.  I also had to ensure all caches of read-only data were flushed and re-loaded on switch, including those lists used as data providers for combobox pick lists.  You will also need to decide what to do with already loaded edit screens.

Best of luck,
Fintan

Copyright (c) Marimer LLC