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
我想在IIS中声明我的连接字符串并从那里获取它。我不想在web.config页面中声明它。相反,我需要知道是否有可能从web.config中的iis获取字符串或从代码文件中读取它。我使用的是asp.net 4.0。在c#编码,服务器是IIS7.5
2 个解决方案
#1
3
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).
根据这篇文章,您可以使用IIS UI或命令行来修改您的连接字符串,但这只会以任何方式写入web.config文件中的
Also, you can store it in another .config file if you wish, and pull it into your web.config like so
此外,如果您愿意,可以将其存储在另一个.config文件中,并将其拉入您的web.config中
<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 (i.e. in a virtual directory).
如果您希望连接字符串的位置不在您的站点下(即在虚拟目录中),这可能非常有用。
#2
0
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
您应该将您的连接字符串放在web.config中。将连接字符串放在其他位置可能不是可移植的,例如从一台机器移动到一台机器。你也有machine.config
In all, put in the web.config and you can encrypt it if you care about the secrecy
总之,放入web.config,如果你关心保密,你可以加密它
You can also have a separate config file e.g. db.config referenced from your web.config using the configSource
attribute.
你也可以有一个单独的配置文件,例如使用configSource属性从web.config引用的db.config。
<connectionStringss configSource="Configuration\db.config" />
#1
3
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).
根据这篇文章,您可以使用IIS UI或命令行来修改您的连接字符串,但这只会以任何方式写入web.config文件中的
Also, you can store it in another .config file if you wish, and pull it into your web.config like so
此外,如果您愿意,可以将其存储在另一个.config文件中,并将其拉入您的web.config中
<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 (i.e. in a virtual directory).
如果您希望连接字符串的位置不在您的站点下(即在虚拟目录中),这可能非常有用。
#2
0
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
您应该将您的连接字符串放在web.config中。将连接字符串放在其他位置可能不是可移植的,例如从一台机器移动到一台机器。你也有machine.config
In all, put in the web.config and you can encrypt it if you care about the secrecy
总之,放入web.config,如果你关心保密,你可以加密它
You can also have a separate config file e.g. db.config referenced from your web.config using the configSource
attribute.
你也可以有一个单独的配置文件,例如使用configSource属性从web.config引用的db.config。
<connectionStringss configSource="Configuration\db.config" />