ASP.NET web.config中AppSettings和ConnectionStrings的區別

來源:互聯網
上載者:User

在ASP.NET編程中發現,可以用兩種方式寫串連資料字串配置 ,就是AppSettings和ConnectionStrings。

其中AppSettings是ASP.NET1.1時期用的,在.NET Framework
2.0中,新增了ConnectionStrings專門用來擷取設定檔
在ConfigurationManager類中

AppSettings和ConnectionStrings節的資料,使用方法如下:

using System.Configuration; 
public partial class _Default : System.Web.UI.Page  

    protected void Page_Load(object sender, EventArgs e) 
    { 
        Label1.Text = ConfigurationManager.ConnectionStrings["connectionStringsName"].ToString(); 
        Label2.Text = ConfigurationManager.AppSettings["appSettingsName"].ToString(); 
    } 
}

<connectionStrings>和<appSettings>這個的區別
1.<connectionStrings>
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=伺服器名;Initial Catalog=資料庫名;User ID=使用者;Password=密碼"
providerName="System.Data.SqlClient" />
</connectionStrings>


在 
<connectionStrings> 裡,在頁面還可以這樣引用<%$ ConnectionString:Name%>.


2.<appSettings>:

<add
key="connectionstringName" value="data source=伺服器名或IP;initial
catalog=資料庫名;persist security info=False;user
id=使用者;password=密碼;packet size=4096">
</add>

 

注意:

(1)AppSettings 是在2003中常用的,ConnectionStrins是2005中常用的.

(2)使用ConnectionString的好處:

第一:可將連接字串加密,使用MS的一個加密工具即可。

第二:可直接繫結資料原始檔控制,而不必寫代碼讀出來再賦值給控制項。

第三:可方便的更換資料庫平台,如換為Oracle資料庫,只需修改providerName。

(3)寫在 <appSettings >中用System.Configuration.ConfigurationManager.AppSettings["name"]檢索值。

   寫在 <ConnectionStrings>中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]檢索值。

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.