How to change multiple connection string value usi

2019-09-01 20:21发布

问题:

I'm currently handling a .Net system that was developed in Linq for its data retrieval structure. This system was developed by other developers last time and I'm dealing a big problem now.

One of the bad practice (or i just not sure why must the developer do that) i found from this system is that, inside /AppData/DataContext/, there are alot of .dbml files and in the .designer.vb, for example Product.designer.vb file, consists of this piece of code:

Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource    
Public Sub New()
            MyBase.New(Global.System.Configuration.ConfigurationManager.ConnectionStrings("DBconstr").ConnectionString, mappingSource)
            OnCreated
    End Sub

My current major challenge is, I need to change to a new connection string name for all DBconstr and point to another database while keeping the current "Dbconstr" setting in web.config. There are over 400 lines in the entire system I need to find and replace if i have to do it manually. So I need advice if there is any way i can change all hard-coded connection string using one or few direct methods instead of changing all 400 plus lines manually?

I had thought about calling certain method in .Master page and override the value of .ConnectionString for all the child pages but not sure if this is possible.

Please advice. Thanks

回答1:

You should have a look at this: http://blogs.msdn.com/b/webdev/archive/2009/05/04/web-deployment-web-config-transformation.aspx. It explains how to use web.config transformation. It basically allows modifying the value of a setting (ie. connection strings) depending on your solution/build configuration.
Modifying the configuration file "on the fly" is much easier than trying to modify it in the code.

You should also check these two answers I provided on the same topic:

  • How to have different web.config settings for my local machine? - https://stackoverflow.com/a/19294499/375304
  • Applying web.config transformations locally - https://stackoverflow.com/a/19301084/375304