zoukankan      html  css  js  c++  java
  • dotNet 取得配置文件的连接信息

    1、ASP.NET 取得Web.config 的配置项

    Web.config

    <configuration>
      <connectionStrings>
        <add name="OraAspNetConString" connectionString="User Id=yourId;Password=yourPwd;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.6)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=pdborcl)))" providerName="Oracle.Web"/>
      </connectionStrings>
      <system.web>
        <compilation debug="true" targetFramework="4.6">
          <assemblies>
            <add assembly="Oracle.Web, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89B483F429C47342"/>
          </assemblies>
        </compilation>
        <httpRuntime targetFramework="4.6"/>
      </system.web>
      <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE="Web" /optionInfer+"/>
        </compilers>
      </system.codedom>
    </configuration>
    

      

    代码侧的取值调用

    string connString = ConfigurationManager.ConnectionStrings["OraAspNetConString"].ConnectionString;
    string connString = WebConfigurationManager.ConnectionStrings["OraAspNetConString"].ToString();
    

      

    2、Winform 取得App.config 的配置项

    App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
        </startup>
      <appSettings>
        <add key="AccessDB" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=....DBDatabase.mdb;Persist Security Info=False" />
        <add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source=....DBDatabase.mdb" />
        <add key="dbPath" value="....DBDatabase.mdb" />
        <add key="ClientSettingsProvider.ServiceUri" value="" />    
      </appSettings>
    </configuration>
    

    写一个取得数据的函数

            /// <summary>
            /// 获取配置文件里appSettings的数据
            /// </summary>
            /// <param name="str">键</param>
            /// <returns>值</returns>
            public static string GetAppSettingsStr(string str)
            {
                AppSettingsReader appReader = new AppSettingsReader();
                return appReader.GetValue(str, typeof(string)).ToString();
            }
    

      

    代码侧的取值调用

    //连接字符串 
    static string connStr = GetAppSettingsStr("SQLConnString");
    

      

  • 相关阅读:
    spoj 104 Highways (最小生成树计数)
    bzoj 1912 巡逻(树直径)
    BZOJ 3534 重建
    BZOJ 3143 游走(高斯消元)
    在Windows下编译PyCaffe
    caffe的Matlab接口的使用方法
    NewRelic性能监控之APM
    MariaDB-10.x二进制包安装
    mongoDB-3.x Balancer Management
    mongoDB-3.x集群管理
  • 原文地址:https://www.cnblogs.com/wzihan/p/14780703.html
Copyright © 2011-2022 走看看