0

I have a situation where I have a vb.net program that uses two connection strings.

This program 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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Some of these other files will already have an app.config file and I do not want to edit anything to do with the sites I am dropping them into. Can I have a separate .config file within my own folder?
@TGuimond - You can have a separate file, but not without changing the existing .config, and possibly make changes to the application as well. I don't see why you resist changing the config file as this is what it is for.
I understand what you mean but I do not have access to each of these application envinronments directly and it is a different person I have to send the code to for each different organisation All of these organisations are very security concious and do not allow outside developers to edit anything within their web applications. It is simply a lot less hassle to zip up a folder and tell them to drop it in their application as opposed to asking them to edit .config files, etc.
@TGuimond - Then provide them with instructions so they can edit the configuration themselves. I still don't understand how the application is supposed to just pick up such information and use it without being changed to do so. Perhaps you need to review your question and explain what you are trying to achieve, instead of asking of one way you think you can achieve it.
Build the application to reference a generic connectionString name like databaseString or something of the like. Zip it up and send it to one group. Then edit your connectionString for the second environment and zip it up and then send it to them. I could understand not allowing access to production data (Database).. but to not allow it to the web.config? Seems like your security manager is more interested in job security than common sense. What happens when your upgrade your datasource and it changes? If they were that worried about security they would encrypt the config section anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.