从web.config中读取数据库连接串(有3两种方式)

1、  利用System.Web.Configuration

读取代码:

String strconn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AddressBookConnectionString"].ConnectionString;

string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["AddressBookConnectionString"].ConnectionString;

web.config中的配置代码

  <connectionStrings>

    <add name="AddressBookConnectionString" connectionString="Data Source=PC- 20090911ZYIV;Initial Catalog=addressBook;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />

  </connectionStrings>

2、  利用System.Configuration

读取代码:

string strconn = System.Configuration.ConfigurationManager.AppSettings["AddressBookConnectionString"].ToString();

web.config中的配置代码

  <appSettings>

    <add key="AddressBookConnectionString" value="Data Source=PC-20090911ZYIV;Initial Catalog=addressBook;User ID=sa;Password=sa"/>

  </appSettings>

你可能感兴趣的:(数据库,String)