Storing database settings outside app.config

主宰稳场 提交于 2019-12-23 05:13:49

问题


I've been writing a c# exe that will be running on a live web server, but I want to be able to test it on our staging server. Since the staging server has different database settings (in the app.config) from my localhost, is there any way I can store the connection string outside the app.config so that I can easily get to it?

Also, is it possible to store the database connection string, then access it via the app.config? This might sound odd, but I'm using a dll from a CMS that uses the value in the .config file.


回答1:


You can "externalize" any .NET config section into a separate file, and reference it from app.config.

So in your case, you would have something like this in app.config:

<connectionStrings configSource="connectionstrings.dev.config" />

and then create separate configs for e.g. DEV, TEST, PROD environments, with different names. Their content would be:

<?xml version="1.0"?>
<connectionStrings>
   -- your connection string here, as normal
</connectionStrings>

You can any any number of those separate files, and switching only means changing a single line in your main app.config to reference the appropriate external connectionstrings.ABC.config file.




回答2:


How about using different app.config in each location?




回答3:


Instead of that, how about creating database settings in app.config with prefixes, such as DevConnectionString and ProdConnectionString. Then, create one more app.config element that tells you which environment you're in.

During development, that environment setting is "Dev". When you deploy your code, change the environment element in app.config to "Prod".

When you want to retrieve the database settings in code, you first examine the environment value, and then choose the database settings accordingly.

One variation on this theme is to create an environment variable in Windows on each machine, and interrogate that in your code.




回答4:


You can store it where you want: In a textfile, in an environment variable, pass it as a command line argument.

You just have to write code to read the connection string and pass it to your dataaccess.

When you tell us how you access you database, i can show you how to pass the connectionstring.

But maybe its better to use different configurationfiles per environment.




回答5:


You can roll you own config for connection strings, call it in appropriate to the current environment, e.g. dev.config, qa.config, prod.config, etc and keep it's name into App.config.

In addition, you can add a record to Machine.config, e.g. called Environment and consider it on reading App.config to determine environment config, i.e:

Machine.config:

<environment name="Dev" />

App.config:

<environments>
    <environment name="Dev" config="dev.config" />
    <environment name="QA" config="qa.config" />
    ...
</environments>



回答6:


That depends on how you are using it. It's just a string, so basically you can store it anywhere you like, but if you are using some library that expects the connection string to be in app.config, you would have to find a different way of supplying the string to it.

The app.config file is however easy to get to. It's just a text file in XML format, so you can edit using any text editor, like Notepad for example. It's not named app.config once it's deployed though, it gets it's filename from the name of the application, with the extension .config.



来源:https://stackoverflow.com/questions/4555550/storing-database-settings-outside-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!