如何从代码隐藏中读取 app.config 中的自定义部分?
我有一个 app.config
文件,它将值存储在几个不同的部分中。我有这些片段:
<configuration>
<configSections>
<sectionGroup name="someDataAccessLayer">
<section name="databaseConnectionStrings" type="sometype" />
</sectionGroup>
</configSections>
<someDataAccessLayer>
<databaseConnectionStrings>
<databaseConnectionString name="someSQL"
value="database=somedatabase;Integrated Security=False;User Id=sa;server=someserver;Password=somepassword/>
</databaseConnectionStrings>
</someDataAccessLayer>
如何读取代码隐藏中的连接字符串?具体来说,值
是
database=somedatabase;Integrated Security=False;User Id=sa;server=someserver;Password=somepassword
感谢您的帮助!如果问题仍然不清楚,请告诉我。
I have an app.config
file that stores values in a few different sections. I have these snippets:
<configuration>
<configSections>
<sectionGroup name="someDataAccessLayer">
<section name="databaseConnectionStrings" type="sometype" />
</sectionGroup>
</configSections>
<someDataAccessLayer>
<databaseConnectionStrings>
<databaseConnectionString name="someSQL"
value="database=somedatabase;Integrated Security=False;User Id=sa;server=someserver;Password=somepassword/>
</databaseConnectionStrings>
</someDataAccessLayer>
How do I read the connection string in the codebehind? Specifically the value
which is
database=somedatabase;Integrated Security=False;User Id=sa;server=someserver;Password=somepassword
Thanks for your help! Please let me know if the question is still unclear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的配置部分将与一些 .NET 类关联来处理它:
因此,要从
部分读取设置,您需要使用ConfigurationManager
(添加对项目的 System.Configuration 的引用)将这些设置放入该类的实例中:现在您有一个
sometype
类型的对象,其中包含该类中的所有设置配置部分。这些属性之一是数据库连接字符串列表,您现在可以枚举并找到合适的字符串并读取其.Value
属性。Your configuration section will be associated with some .NET class to handle it:
So to read the settings from the
<localeSettings>
section, you need to use theConfigurationManager
(add a reference toSystem.Configuration
to your project) to get those settings into an instance of that class:Now you have an object of type
sometype
that contains all the settings in that config section. One of those properties will be a list of database connection strings, which you can now enumerate and find the appropriate one and read it's.Value
property.App.Config 设置:
使用
ConfigurationManager
在 DataLayer 访问:App.Config Settings:
Access at DataLayer using the
ConfigurationManager
as: