設定檔中AppSettings和ConnectionStrings的區別

來源:互聯網
上載者:User

標籤:設定檔   winform   

在winform中添加的設定檔,自動產生的為App.config,而在Web中,自動產生的設定檔為Web.config。
在跟著牛腩敲代碼的時候,就跟著視頻中的代碼一步一步的來,並沒有發現偷偷的把設定檔給換了類型了。在又一次對winform中的設定檔操作的時候,直接複製了Web.config中寫的代碼,而在代碼中卻用的是AppSettings來弄的設定檔。成了牛頭不對馬嘴,不可避免的報錯了。這時我才發現設定檔原來有兩種。
現在看看兩者的區別:

**AppSettings** <configuration> <AppSettings>    <add key="ConnectionString" value="Data Source=.;Initial Catalog=Test;User Id=sa;Password=123;" />    </AppSettings></configuration>**ConnectionStrings**<ConnectionStrings>    <add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123;" providerName="System.Data.SqlClient"/></ConnectionStrings>

兩者的區別:
1、 AppSettings連接字串的屬性名稱為“key”,在ConnectionStrings中連接字串的屬性名稱為“name”。
2、 檢索值不同
ConnectionStrings所使用如下代碼來檢索值
System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString;
而AppSettings通過如下代碼來檢索值
System.Configuration.ConfigurationManager.AppSettings[“ConnectionString”]

使用ConnectionString的好處
1、 可將連接字串加密
2、 可直接綁定資料來源控制項,而不必寫代碼讀出來再賦值給控制項;
3、 可方便的更換資料庫平台。
4、 ConnectionStrings最主要用於資料庫連接,而AppSettings主要用於Asp.net應用程中的一些配置資訊。比如上傳檔案的路徑等。

對於設定檔的使用,我還是只知道了那麼個皮毛。看了一篇寫設定檔的部落格,大部分都看不懂,好高大上的樣子…先留起來,後面說不定會用到。
參考部落格連結:http://zhoufoxcn.blog.51cto.com/792419/166441

設定檔中AppSettings和ConnectionStrings的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.