简体   繁体   中英

connectionstring declare in iis without declaring in web.config

I want to declare my connection string in IIS and get it from there. I don't want to declare it in web.config page. Rather I need to know if is it possible to get the string from iis in web.config or read it from code file. I am using asp.net 4.0.,coding in c# and server is IIS7.5

According to this article you can use the IIS UI, or the command line to modify your connection string, but this will just write into the <connectionString> element in the web.config file any way (unless you've set it up to save elsewhere).

Also, you can store it in another .config file if you wish, and pull it into your web.config like so

<appSettings file="../VirtualDirectory/config/Env.config">
</appSettings>

You could then call it like so in your code:

System.Configuration.ConfigurationManager.AppSettings["DefaultConn"]

This can be quite useful if you want the location of your connection string to not be under your site (ie in a virtual directory).

You should put your connection string in the web.config. Putting your connection string elsewhere might not be portable, like moving from machine to machine. You also have the machine.config

In all, put in the web.config and you can encrypt it if you care about the secrecy

You can also have a separate config file eg db.config referenced from your web.config using the configSource attribute.

<connectionStringss configSource="Configuration\db.config" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM