C# 读取配置文件 app.config, web.config

添加配置文件:App.config or Web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!--connectionStrings-->
  <connectionStrings>
    <add name="DBConnString1" connectionString="Data Source=127.0.0.1;Initial Catalog=test;uid=sa;pwd=123456789"/>
  </connectionStrings>

  <!--appSettings-->
  <appSettings>
    <add key="DBConnString2" value="Data Source=127.0.0.1;Initial Catalog=test;uid=sa;pwd=123456789" />
  </appSettings>
</configuration>


读取

引用:using System.Configuration;

string conn1 = ConfigurationManager.ConnectionStrings["DBConnString1"].ConnectionString;
string conn2 = ConfigurationManager.ConnectionStrings["DBConnString1"].ToString();
string conn3 = ConfigurationManager.AppSettings["DBConnString2"];
作者最新文章
ASP.NET, ASP.NET Core WebApi 返回 Controller 返回 Json 类型结果
Visual Studio 调试 ASP.NET Core 无法连接 IIS Express 服务器
ASP.NET WebApi 与 ASP.NET Core WebApi 返回 HttpResponseMessage
SQL Server 中 CASE WHEN 使用
html table 设置表格背景图片,背景色