如何从代码隐藏中读取 app.config 中的自定义部分?

发布于 2024-12-23 12:47:54 字数 841 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-12-30 12:47:54

您的配置部分将与一些 .NET 类关联来处理它:

  <configSections>
    <sectionGroup name="someDataAccessLayer">
       <section name="databaseConnectionStrings" type="sometype" />
    </sectionGroup>
  </configSections>

因此,要从 部分读取设置,您需要使用 ConfigurationManager (添加对项目的 System.Configuration 的引用)将这些设置放入该类的实例中:

sometype cs = ConfigurationManager.GetSection("someDataAccessLayer/databaseConnectionStrings") as     sometype;

现在您有一个 sometype 类型的对象,其中包含该类中的所有设置配置部分。这些属性之一是数据库连接字符串列表,您现在可以枚举并找到合适的字符串并读取其 .Value 属性。

Your configuration section will be associated with some .NET class to handle it:

  <configSections>
    <sectionGroup name="someDataAccessLayer">
       <section name="databaseConnectionStrings" type="sometype" />
    </sectionGroup>
  </configSections>

So to read the settings from the <localeSettings> section, you need to use the ConfigurationManager (add a reference to System.Configuration to your project) to get those settings into an instance of that class:

sometype cs = ConfigurationManager.GetSection("someDataAccessLayer/databaseConnectionStrings") as     sometype;

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.

我恋#小黄人 2024-12-30 12:47:54

App.Config 设置:

<configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="MyApp.LocalConnectionString"
                connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=DBName;Integrated Security = true"
                providerName="System.Data.SqlClient" />

使用 ConfigurationManager 在 DataLayer 访问:

// add reference 
using  System.Configuration;
// then access connection string in your class
 private static string strConnectionString = ConfigurationManager.ConnectionStrings["MyApp.LocalConnectionString"].ConnectionString;  

App.Config Settings:

<configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="MyApp.LocalConnectionString"
                connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=DBName;Integrated Security = true"
                providerName="System.Data.SqlClient" />

Access at DataLayer using the ConfigurationManager as:

// add reference 
using  System.Configuration;
// then access connection string in your class
 private static string strConnectionString = ConfigurationManager.ConnectionStrings["MyApp.LocalConnectionString"].ConnectionString;  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文