I have a situation where I have a vb.net program that uses two connection strings.
This progra开发者_如何学Pythonm will be used on multiple web servers on multiple domains and the only difference for any of them is the two connection strings.
What would be the best solution to read in these connection strings from a file separate from my actual .vb code?
Ideally I want to be able to drop the .vb and the connection string file into its own folder on any number of asp.net websites and not update anything else (for example add any connections to the web.config or anything) than the external file containing the connection strings, I assume something like xml would suffice?
If possible could you give me code examples?
I would put them both in the same web.config. Give them two different names and dependent on which server you are on. You can use: Request.ServerVariables("SERVER_NAME") -OR- Request.ServerVariables("LOCAL_ADDR") Depending on your expected return you can condition the webconfigurationmanager to read one of the two connectionStrings property. Your best bet is to set a Session Variable from a basepage. Condition it to see if the Session has been populated and thus, you can read the variable throughout the application.
If you use two different .config files you are going to run into a problem where you are going to have to code your entire application on which one you want to ALWAYS use depending on which server you are on.
If you include a web.config file inside another web.config file it is the same as just adding the same two different connectionStrings in the same web.config file.
Use the built in .config
support.
If you add an app.config
file to your project, you can add a connectionStrings
section to it, which you can access using the ConfigurationManager.ConnectionStrings
property.
Please don't try to reinvent the wheel.
精彩评论