ASP.NET設定檔中AppSettings和ConnectionStrings的區別

來源:互聯網
上載者:User

在.NET Framework 2.0中,ConfigurationManager類新增了兩個屬性AppSettings和ConnectionStrings專門用來擷取設定檔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="SYXTConnectionString" connectionString="Data Source=LEIKE;Initial Catalog=SYXT;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>

2.<appSettings>:

<add key="connectionstring1" value="data source=192.168.123.201;initial catalog=webmedlink;persist security info=False;user id=sa;password=KSDChagd2004;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.