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=..\..\DB\Database.mdb;Persist Security Info=False" />
    <add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source=..\..\DB\Database.mdb" />
    <add key="dbPath" value="..\..\DB\Database.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");

  

dotNet 取得配置文件的连接信息

上一篇:Jmeter 请求元件 之 HTTP请求默认值


下一篇:web端测试点