使用webconfig,联动配置程序

只需要在appSettings中指定当前使用哪个系统,而不用到处配置

只需要访问自定义的配置文件,就可以读取所有的节点数据

第一步:


  <configSections>
    <sectionGroup name="AllApp">
      <section name = "App_1" type="System.Configuration.SingleTagSectionHandler"/>
      <section name = "App_2"  type = "System.Configuration.SingleTagSectionHandler" />
      <section name = "App_3"  type = "System.Configuration.SingleTagSectionHandler" />
    </sectionGroup>
  </configSections >

  <AllApp>
    <JIT SystemID="App_1"
         SystemTitle="系统_1"
         ConnectionString="Data Source=192.1.1.1;Initial Catalog=DB_1;User ID=sa;Password=1"/>
    <PIT SystemID="App_2"
         SystemTitle="系统_2"
         ConnectionString="Data Source=192.1.1.1;Initial Catalog=DB_2;User ID=sa;Password=1"/>
    <PKT SystemID="App_3"
         SystemTitle="系统_3"
         ConnectionString="Data Source=192.1.1.1;Initial Catalog=DB_3;User ID=sa;Password=1"/>
  </AllApp>

  <appSettings>
    <add key="CurrentApp" value="AllApp/JIT"/>
  </appSettings>
  第二步:

public class MyConfig
{
    static Hashtable table = null;
    public static void Init()
    {
        table = new Hashtable();
        string app = System.Configuration.ConfigurationManager.AppSettings["CurrentApp"];
        table = (Hashtable)ConfigurationManager.GetSection(app);
    }

    public static string SystemID
    {
        get
        {
            if (table == null)
                Init();
            return table["SystemID"].ToString();
        }
    }
    public static string SystemTitle
    {
        get
        {
            if (table == null)
                Init();
            return table["SystemTitle"].ToString();
        }
    }
    public static string ConnectionString
    {
        get
        {
            if (table == null)
                Init();
            return table["ConnectionString"].ToString();
        }
    }
}

第三步:


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string systitle = MyConfig.SystemTitle;

原文地址:https://www.cnblogs.com/zzlchn/p/1850624.html