.NET AppSettings與ConnectionStrings使用案例詳解
AppSettings是ASP.NET1.1時(shí)期用的,在.NET Framework 2.0中,新增了ConnectionStrings.
1.ConnectionStrings的使用
<connectionStrings> <add name="ConnectionStringName" connectionString="Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫名;User ID=用戶;Password=密碼" providerName="System.Data.SqlClient" /> </connectionStrings>
或者:
<connectionStrings> <add name="ConnectionStringName" connectionString="sever=服務(wù)器名;database=數(shù)據(jù)庫名;User ID=用戶;Password=密碼" providerName="System.Data.SqlClient" /> </connectionStrings>
在頁面還可以這樣引用<%$ ConnectionString:Name%>.
2.<appSettings>的使用
<add key="connectionstringName" value="data source=服務(wù)器名或IP;initial catalog=數(shù)據(jù)庫名;persist security info=False;user id=用戶;password=密碼;packet size=4096"> </add>
3.區(qū)別
1)AppSettings 是在2003中常用的,ConnectionStrins是2005中常用的.
2)使用ConnectionString的好處:
- 第一:可將連接字符串加密,使用MS的一個(gè)加密工具即可。
- 第二:可直接邦定的數(shù)據(jù)源控件,而不必寫代碼讀出來再賦值給控件。
- 第三:可方便的更換數(shù)據(jù)庫平臺,如換為Oracle數(shù)據(jù)庫,只需修改providerName。
3)寫在 <appSettings >中用System.Configuration.ConfigurationManager.AppSettings["name"]檢索值。
寫在 <ConnectionStrings>中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]檢索值。
4.測試
在VS2005中新建一個(gè)網(wǎng)站,然后再defaul頁面中加入如下代碼
using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { labConn.Text = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ToString(); labApp.Text = ConfigurationManager.AppSettings["SiteSqlServer"].ToString(); } }
而web.config的代碼如下:
<?xml version="1.0"?> <!-- 注意: 除了手動(dòng)編輯此文件以外,您還可以使用 Web 管理工具來配置應(yīng)用程序的設(shè)置。可以使用 Visual Studio 中的 “網(wǎng)站”->“Asp<a rel="external nofollow" class='replace_word' title=".NET知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a> 配置”選項(xiàng)。 設(shè)置和注釋的完整列表在 machine.config.comments 中,該文件通常位于 /Windows/Microsoft.Net/Framework/v2.x/Config 中 --> <configuration> <connectionStrings> <add name="SiteSqlServer" connectionString="Data Source=XUWEI/SQLEXPRESS;Initial Catalog=store;User ID=dnndemo;Password=dnndemo" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="SiteSqlServer" value="Data Source=XUWEI/SQLEXPRESS;Initial Catalog=store;User ID=dnndemo;Password=dnndemo" /> </appSettings> <system.web> <!-- 設(shè)置 compilation debug="true" 將調(diào)試符號插入 已編譯的頁面中。但由于這會(huì) 影響性能,因此只在開發(fā)過程中將此值 設(shè)置為 true。 --> <compilation debug="true"/> <!-- 通過 <authentication> 節(jié)可以配置 ASP.NET 使用的 安全身份驗(yàn)證模式, 以標(biāo)識傳入的用戶。 --> <authentication mode="Windows"/> <!-- 如果在執(zhí)行請求的過程中出現(xiàn)未處理的錯(cuò)誤, 則通過 <customErrors> 節(jié)可以配置相應(yīng)的處理步驟。具體說來, 開發(fā)人員通過該節(jié)可以配置 要顯示的 html 錯(cuò)誤頁 以代替錯(cuò)誤堆棧跟蹤。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web> </configuration>
當(dāng)然前提是在編輯頁面中添加了兩個(gè)lable,分別為labConn和labApp。
string conString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
到此這篇關(guān)于.NET AppSettings與ConnectionStrings使用案例詳解的文章就介紹到這了,更多相關(guān).NET AppSettings與ConnectionStrings使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection 集群場景)下篇
這篇文章主要為大家再一次介紹了ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09.NET讀寫Excel工具Spire.Xls使用 重量級的Excel圖表功能(5)
這篇文章主要為大家詳細(xì)介紹了.NET讀寫Excel工具Spire.Xls使用,重量級的Excel圖表功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Asp.net實(shí)時(shí)顯示文本框字?jǐn)?shù)實(shí)現(xiàn)代碼
實(shí)時(shí)顯示文本框字?jǐn)?shù)在日常開發(fā)中很常見,也很實(shí)用,接下來為大家介紹下如何實(shí)現(xiàn)實(shí)時(shí)顯示,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04ASP.NET配合jQuery解決跨域調(diào)用的問題
這篇文章主要介紹了ASP.NET配合jQuery解決跨域調(diào)用的問題,簡單實(shí)用,需要的朋友可以參考下。2016-06-06.NET事件監(jiān)聽機(jī)制的局限與擴(kuò)展分析
這篇文章主要介紹了.NET事件監(jiān)聽機(jī)制的局限與擴(kuò)展,詳細(xì)分析了.NET事件監(jiān)聽機(jī)制的機(jī)制與優(yōu)劣,有助于更好的理解.NET的運(yùn)行原理,需要的朋友可以參考下2014-11-11