c#引用web.config中的ConnectionString

时间:2024-12-25 08:34:20

c#引用web.config中的ConnectionString

<connectionStrings>
  <add name="JKXTConnectionString" connectionString="Data Source=CHF;Initial Catalog=JKXT;User ID=sa" providerName="System.Data.SqlClient" />
 
<add name="JGXTConnectionString" connectionString="Data
Source=CHF;Initial Catalog=JGXT;User ID=sa"
providerName="System.Data.SqlClient" />
 </connectionStrings>

<connectionStrings>
    <add name="DB_EMSConnectionString" connectionString="Data Source=85DDBBBFA5A34F9;Initial Catalog=DB_EMS;Integrated Security=True" providerName="System.Data.SqlClient" />

</connectionStrings>

获取的办法是:
ConfigurationManager.ConnectionStrings["JGXTConnectionString"].ConnectionString;

或者这么写web.config
 <appSettings>
  <add key="connectionstring" value="Data Source=CHF;Initial Catalog=JGXT;User ID=sa"/>
 </appSettings>

<appSettings>

<add key="DbHelperProvider" value="System.Data.SqlClient"/>

<add key="DbHelperConnectionString" value="Data Source=85DDBBBFA5A34F9;Initial Catalog=DB_EMS;Integrated Security=True"/>

</appSettings>

获取办法是:
ConfigurationSettings.AppSettings["JGXTConnectionString"];

引用using System.Configuration;