版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、<p> 在SQL Server數(shù)據(jù)庫(kù)里存儲(chǔ)Session</p><p> HTTP是個(gè)狀態(tài)很不確定的協(xié)議,為了允許用戶通過(guò)請(qǐng)求保存狀態(tài)信息,ASP.NET提供了Session存儲(chǔ)機(jī)制。這些Session變量按每個(gè)用戶被存儲(chǔ)起來(lái)。在傳統(tǒng)的ASP里,你只能在Web服務(wù)器的內(nèi)存里暫時(shí)存儲(chǔ)Session變量,但是這個(gè)方法已經(jīng)被證明了在擴(kuò)展性和可依賴性上的不足。在ASP.NET里,你可以為你的每個(gè)請(qǐng)求定制
2、Session狀態(tài)存儲(chǔ)。本文將探討存儲(chǔ)Session變量可伸縮性和可靠性都很好的方式之一的SQL Server 。</p><p> 在傳統(tǒng)的ASP里 ,默認(rèn)的Session狀態(tài)保存在服務(wù)器的內(nèi)存中。 但是,這種做法帶來(lái)兩方面的問(wèn)題:</p><p> (1)它讓服務(wù)器超負(fù)荷,影響了網(wǎng)站服務(wù)器的伸縮性能。</p><p> ?。?)它不能有效地應(yīng)用于Web服務(wù)器
3、群。</p><p> 讓我在一些細(xì)節(jié)上討論這些問(wèn)題,使你能為你選擇了Session存儲(chǔ)感到慶幸。 </p><p> Session變量依據(jù)每個(gè)用戶為基礎(chǔ)生成。 默認(rèn)情況下,它們都保留在Web服務(wù)器的內(nèi)存中。 想象一個(gè)有著成千上萬(wàn)用戶的網(wǎng)站。 由于巨大的用戶數(shù)量, Web服務(wù)器存儲(chǔ)的活躍Session的數(shù)目也將非常的多。 這意味著你存放著非常多的session數(shù)據(jù)在Web服務(wù)器的內(nèi)存
4、中。 如果不斷的對(duì)服務(wù)器增加負(fù)載, 它可能達(dá)到飽和,以至造成對(duì)應(yīng)用程序整體擴(kuò)展性能上的不良影響。 </p><p> 為了解決這個(gè)影響到擴(kuò)展性能的問(wèn)題,實(shí)現(xiàn)Web集群。所謂的 Web集群是一組網(wǎng)絡(luò)服務(wù)器并行運(yùn)行, 服務(wù)器集群里的每個(gè)Web服務(wù)器都有您的網(wǎng)站的一個(gè)鏡像。您的網(wǎng)站的流通負(fù)載平均分配給每個(gè)可用的服務(wù)器,從而達(dá)到負(fù)載平衡。 在Web服務(wù)器的內(nèi)存里存儲(chǔ)Session變量會(huì)阻礙Web集群的建立,下面將舉例來(lái)
5、說(shuō)明: </p><p> 假定有三個(gè)Web服務(wù)器:S1 , S2 ,和S3 。并行地連接在一起接受用戶請(qǐng)求。 假定這個(gè)時(shí)候有一個(gè)請(qǐng)求R1來(lái)到服務(wù)器集群并且負(fù)載平衡邏輯判定 S2,S3都因?yàn)槟承┢渌娜蝿?wù)而沒(méi)有空閑,但是S1可以處理這個(gè)請(qǐng)求。很顯然,這個(gè)請(qǐng)求會(huì)被送到S1進(jìn)行處理?,F(xiàn)在,想象在這個(gè)處理過(guò)程當(dāng)中你在S1的內(nèi)存中存儲(chǔ)了某個(gè)Session變量。到目前為止,一切還很好。過(guò)了一些時(shí)間,同樣的用戶有了另一個(gè)請(qǐng)
6、求R2,這個(gè)請(qǐng)求需要上一個(gè)請(qǐng)求所存儲(chǔ)的Session變量。但是這個(gè)時(shí)候S1已經(jīng)被一些任務(wù)使用著,而S2,S3卻處于空閑的狀態(tài)。你可以猜到根據(jù)每一條的負(fù)載平衡規(guī)則,R2將會(huì)被送到S2進(jìn)行處理。但是,如果那發(fā)生了,S2怎么能夠得到Session變量?畢竟,它們存儲(chǔ)在和S2完全沒(méi)有任何關(guān)聯(lián)的Web服務(wù)器S1的內(nèi)存中。這意味著R2仍然得等待S1的空閑。這種事情很顯然是Web服務(wù)器群資源的浪費(fèi)。 </p><p> AS
7、P.NET 2.0及其Session信息存儲(chǔ)</p><p> ASP.NET 2.0允許你存儲(chǔ)session變量在三個(gè)不同地方:</p><p> ?。?)在網(wǎng)站服務(wù)器的內(nèi)存中存儲(chǔ)(進(jìn)程中的)。 </p><p> ?。?)在一臺(tái)專門用來(lái)存放Session變量的服務(wù)器中存儲(chǔ)。( Session狀態(tài)信息存儲(chǔ)服務(wù)器 )</p><p> ?。?/p>
8、3)在一個(gè)SQL Server的數(shù)據(jù)庫(kù)里面存儲(chǔ)。</p><p> 默認(rèn)情況是第一種存儲(chǔ)模式。 </p><p> 模式二和模式三通常被稱為“進(jìn)程外”的模式,因?yàn)镾ession變量的存儲(chǔ)和Web站點(diǎn)沒(méi)有關(guān)系。 在這篇文章,你將被限制在只探索第三種模式。在SQL Server數(shù)據(jù)庫(kù)里存儲(chǔ)Session變量具有以下優(yōu)點(diǎn): </p><p> ?。?)可擴(kuò)展性: 如果
9、您正在找一個(gè)高度可擴(kuò)展性的方法來(lái)儲(chǔ)存Session變量 ,SQL Server正是如此,它比其它的可擴(kuò)展性更好。 Web服務(wù)器群可以很容易地獲取Session變量,因?yàn)樗鼈兌純?chǔ)存在一個(gè)獨(dú)立的數(shù)據(jù)庫(kù)。</p><p> (2)可靠性:因?yàn)閿?shù)據(jù)是很真實(shí)的存放在數(shù)據(jù)庫(kù)里面,它比其他方法都要好。不用去擔(dān)心服務(wù)器重啟的問(wèn)題。</p><p> ?。?)安全: SQL Server比在內(nèi)存或Sess
10、ion狀態(tài)信息存儲(chǔ)服務(wù)器中更安全。 你可以更簡(jiǎn)單的通過(guò)配置SQL Server的安全選項(xiàng)來(lái)保護(hù)你的數(shù)據(jù)。</p><p> 在web.config 文件里,可以通過(guò)定義一個(gè)<session State>來(lái)配置Session狀態(tài)模式 。 </p><p><b> 備注:</b></p><p> 如果是在Web服務(wù)器群里, W
11、eb服務(wù)器群的每一臺(tái)服務(wù)器的IIS METABASE的應(yīng)用程序路徑應(yīng)該是完全相同的。Session結(jié)束事件不會(huì)因?yàn)槿魏芜M(jìn)程之外的模式發(fā)生。</p><p> 配置SQL Server來(lái)儲(chǔ)存Session狀態(tài)</p><p> 在你真正的在SQL Server存儲(chǔ)Session狀態(tài)之前, 你需要先配置它。 配置工作都是通過(guò)一個(gè)名為aspnet_regsql.exe的命令行工具來(lái)完成的。
12、</p><p> 在SQL Server里面,你可以在三個(gè)可能的地點(diǎn)存儲(chǔ)Session狀態(tài),</p><p> (1)臨時(shí)存儲(chǔ): 在這種情況下,Session狀態(tài)保存在SQL Server的tempdb 數(shù)據(jù)庫(kù)里面。 該工具建立一個(gè)數(shù)據(jù)庫(kù)名為aspstate并增加特定的存儲(chǔ)過(guò)程來(lái)管理要保存的Session信息。 這個(gè)工具還創(chuàng)建tempdb 數(shù)據(jù)庫(kù)所需的表。 如果您重新啟動(dòng)SQL Se
13、rver服務(wù),Session信息不會(huì)丟失。</p><p> (2)持久存儲(chǔ): 該工具建立一個(gè)數(shù)據(jù)庫(kù)名為aspstate并增加特定的存儲(chǔ)過(guò)程來(lái)管理要保存的Session信息。 Session狀態(tài)信息儲(chǔ)存在aspstate數(shù)據(jù)庫(kù)里。 這種方法的優(yōu)點(diǎn)是,即使您重新啟動(dòng)的SQL Server 。 Session信息也不會(huì)丟失。</p><p> (2)定制存儲(chǔ):Session狀態(tài)信息和數(shù)據(jù)存
14、儲(chǔ)過(guò)程都有用戶定制存儲(chǔ)。 同時(shí)數(shù)據(jù)庫(kù)的名稱必須在配置文件中聲明。 </p><p> 下面的表列出了這個(gè)工具與Session存儲(chǔ)配置相關(guān)的各種命令行開(kāi)關(guān):</p><p> 表單和創(chuàng)建的存儲(chǔ)過(guò)程</p><p> 作為一個(gè)開(kāi)發(fā)人員, 你可能從來(lái)無(wú)法直接影響session狀態(tài)數(shù)據(jù)庫(kù)。 然而, 當(dāng)你運(yùn)行ASPNET_REGSQL.EXE 工具, 你將會(huì)對(duì)表單和數(shù)據(jù)
15、庫(kù)的創(chuàng)建會(huì)有一個(gè)很好的理解。</p><p> 配置 1:在SQL數(shù)據(jù)庫(kù)服務(wù)器中創(chuàng)建表單</p><p> 配置 2:在數(shù)據(jù)庫(kù)服務(wù)器中創(chuàng)建存儲(chǔ)過(guò)程</p><p> 配置你的網(wǎng)站來(lái)存儲(chǔ)Session 狀態(tài)</p><p> 一旦你配置你的SQL服務(wù)器來(lái)支持session狀態(tài)存儲(chǔ),下一步就是配置你的網(wǎng)站了。web.config 的標(biāo)記&
16、lt;sessionState>允許你指定session存儲(chǔ)的信息。下列的表中列出了一些這個(gè)標(biāo)記得重要屬性:</p><p><b> 例子</b></p><p> 你可以開(kāi)發(fā)一個(gè)簡(jiǎn)單的網(wǎng)站,設(shè)置它來(lái)存儲(chǔ)一個(gè)在SQL服務(wù)器數(shù)據(jù)庫(kù)中的sesseion狀態(tài),來(lái)示例你到目前為止所學(xué)的東西。</p><p><b> 設(shè)置SQL
17、服務(wù)器</b></p><p> 打開(kāi)Visual Studio.NET 2005命令提示器,使用一下命令</p><p> aspnet_regsql -ssadd -S .sqlexpress -E -sstype p</p><p> aspnet_regsql -ssadd -S .sqlexpress -E -sstype p</p
18、><p> 現(xiàn)在,你正在使用帶著完整安全屬性的SQL Express數(shù)據(jù)庫(kù)。</p><p> 配置 3 展示了一個(gè)把帶著session 狀態(tài)類型的工具設(shè)置為"persistent"的例子。</p><p> 配置3:運(yùn)行asp.net_regsql.exe工具</p><p><b> 創(chuàng)建和配置一個(gè)網(wǎng)站&
19、lt;/b></p><p> 現(xiàn)在,創(chuàng)建一個(gè)新的網(wǎng)站,修改web.config文件成下列的代碼</p><p> <sessionState mode="SQLServer" sqlConnectionString="datasource=.sqlexpress;</p><p> integrated securi
20、ty=true"></sessionState></p><p> 這里,你添加了這個(gè)<sessionState>標(biāo)記,設(shè)置它的模式屬性為SQLServer。你還要指定sqlConnectionString屬性,把它指向你的SQL SERVER。</p><p> 創(chuàng)建測(cè)試網(wǎng)站的WEB窗體</p><p> 在WEB站點(diǎn)
21、中添加兩個(gè)WEB窗體。這兩個(gè)窗體分別叫page1.aspx和page2.aspx。第一個(gè)WEB窗體將顯示一個(gè)產(chǎn)品列表來(lái)選擇。被選擇的產(chǎn)品被保存在一個(gè)DataSet中。然后通過(guò)保存這個(gè)DataSet </p><p> 在一個(gè)session變量中來(lái)把它在請(qǐng)求中傳遞。第二個(gè)Web窗體簡(jiǎn)單的顯示了來(lái)自DataSet中的被選擇的產(chǎn)品。</p><p> 通過(guò)一下的步驟來(lái)完成第一個(gè)Web 窗體:
22、</p><p> 拖拉一個(gè)GRIDVIEW控件和一個(gè)SQL Data Source控件在WEB FORM上. </p><p> 配置SQL Data Source, 從Northwind 數(shù)據(jù)庫(kù)的Products 表中選擇ProductID, ProductName, 與 </p><p> UnitPrice columns </p>&l
23、t;p> 通過(guò)設(shè)置他的DataSourceID屬性到SQL Data Source控件的ID屬性上來(lái)綁定GridView到SQL Data Source上. </p><p> 在GridView的samrt標(biāo)記上, 點(diǎn)擊"Allow Selection" checkbox. </p><p> 添加一個(gè)HyperLink 控件,設(shè)置NavigateUrl屬
24、性到Page2.aspx. </p><p> 創(chuàng)建一個(gè) DataSet 和 DataTable. FataTable 包含兩個(gè)DataColumns: ProducyName 和 Qty. 然后你存儲(chǔ)一個(gè)session變量"myds".當(dāng)用戶選擇一個(gè)產(chǎn)品的時(shí)候, 你可以創(chuàng)建一個(gè)新的DataRow, 分配產(chǎn)品的資料給它, 添加這個(gè)ROW去DataTable. 用戶的選擇可以在跨多個(gè)請(qǐng)求中持
25、續(xù). </p><p> 按一下的步驟創(chuàng)建第二張的Web form</p><p> 拖拉一個(gè)GridView到Web form上. </p><p><b> 添加兩個(gè)綁定區(qū)域 </b></p><p> 設(shè)置第一個(gè)綁定區(qū)域的HeaderText屬性成"Product Name". 同樣的,
26、設(shè)置DataField成"Product Name". </p><p> 設(shè)置第二個(gè)綁定區(qū)域的HeaderText屬性成"Quantity". 同樣的,設(shè)置DataField成" Qty ". </p><p> 添加下列的代碼到第二張Web Form 的 Page_Load event handler上</p>
27、<p> protected void Page_Load(object sender, EventArgs e)</p><p><b> {</b></p><p> GridView1.DataSource=(DataSet)Session["myds"];</p><p> GridView1.
28、DataBind();</p><p><b> }</b></p><p> 這里,你設(shè)置GridView控件的DataSource屬性到你的儲(chǔ)存在session上的DATASET.然后你通過(guò)調(diào)用DataBind() 方法來(lái)綁定GridView.</p><p> 運(yùn)行WEB FORMS 的例子</p><p>
29、 為了測(cè)試你的WEB FORMS, 設(shè)置Page.aspx 作為你的起始頁(yè), 然后運(yùn)行你的WEB SITE. 你應(yīng)該看到如圖4所展示的:</p><p> Figure 4: 運(yùn)行Page1.aspx 的例子</p><p> 通過(guò)”Add to cart”按鈕選擇一些產(chǎn)品. 這將會(huì)添加一些行在DataTable上. 記得你正在存儲(chǔ)你的DataSet在一個(gè)session變量之中. 然
30、后,通過(guò)點(diǎn)擊”Show mycart” 超鏈接導(dǎo)航到 Page.aspx . 圖5顯示了帶著前面所選的產(chǎn)Page.aspx. </p><p> 配置 5: 運(yùn)行Page2.aspx的例子</p><p> 正如你所見(jiàn)的, Page2.aspx 正確的顯示了你在前頁(yè)所選的那些條目. 這顯示你的session 變量被真正的存儲(chǔ)在了SQL服務(wù)器的數(shù)據(jù)庫(kù)中,并被在第二頁(yè)中找回. 還有, 注意
31、你在無(wú)關(guān)儲(chǔ)存模式的session中用了相同的存儲(chǔ)調(diào)用值得標(biāo)記.</p><p> 儲(chǔ)存Session狀態(tài)在SQL服務(wù)器中的劣勢(shì)雖然存儲(chǔ)session狀態(tài)在SQL 服務(wù)器中可以使你的網(wǎng)站更容易拓展和更穩(wěn)定. 但是這個(gè)技術(shù)有一下的幾個(gè)劣勢(shì)執(zhí)行上: 在執(zhí)行的幾個(gè)項(xiàng)目中,一個(gè)基于SQL SERVER的session 存儲(chǔ)可能是一個(gè)最慢地選擇. 因?yàn)槟愕膕ession變量是存儲(chǔ)在物理的數(shù)據(jù)庫(kù)中.它用了更多的時(shí)候去存儲(chǔ)和讀
32、取數(shù)據(jù)庫(kù). 這會(huì)影響了你的網(wǎng)站的運(yùn)行速度. 花費(fèi): 因?yàn)槟愦鎯?chǔ)你的數(shù)據(jù)在數(shù)據(jù)庫(kù)中, 你需要一個(gè)SQL數(shù)據(jù)庫(kù)的執(zhí)照, 這將給你的網(wǎng)站帶來(lái)額外的開(kāi)銷. 連續(xù)的數(shù)據(jù): 這個(gè)方法要求你所有保存在session變量中的數(shù)據(jù)必須是連續(xù)的如果你想存儲(chǔ)數(shù)據(jù)在一個(gè)session中,這也許強(qiáng)制你去標(biāo)記你自己的類作為[Serializable].</p><p><b> 總結(jié)</b></p>&
33、lt;p> ASP.NET 2.0 可以允許你存儲(chǔ)session狀態(tài)到一個(gè)SQL服務(wù)器的數(shù)據(jù)庫(kù)中。 ASPNET_REGSQL.EXE工具配置了數(shù)據(jù)庫(kù)來(lái)支持這個(gè)特性。</p><p> 更進(jìn)一步, <sessionState>標(biāo)記配置你的網(wǎng)站來(lái)支持這個(gè)模式。 存儲(chǔ)一個(gè)session狀態(tài)在一個(gè)SQL服務(wù)器中是一種更具拓展,安全性和穩(wěn)定性的選擇。雖然, 這種選擇的運(yùn)行相對(duì)于其他選擇而言將會(huì)更慢。
34、</p><p> Storing Session State in a SQL Server Database</p><p> Introduction</p><p> HTTP is a stateless protocol. To allow users save to state information across requests, ASP.N
35、ET provides Session storage. The session variables are stored on per-user basis. In ASP classic, you can store session variables only in the Web server's memory. However, this approach proves to be poor in terms of s
36、calability and reliability. In ASP.NET 2.0, however, you can customize the session state store as per your requirement. This article will explore one of the scalable and reliable approaches f</p><p> As in
37、classic ASP, by default the session state is maintained in the Web server's memory. However, this approach poses two problems:</p><p> It overburdens the server, affecting the Web site's scalability
38、 </p><p> It cannot be used effectively in Web farm scenarios </p><p> Let me discuss these problems in a bit of detail so that you can appreciate your choice of a session store.</p>&l
39、t;p> Session variables are created on a per-user basis. By default, they are maintained in the Web server's memory. Imagine a Web site with thousands of users. Because of the huge number of users, the number of a
40、ctive sessions on the Web server also will be vary high. That means you are storing too much data in the Web server's memory. If the load on the server keeps of increasing, it may reach saturation and cause trouble f
41、or overall scalability of your application.</p><p> To tackle the issue of scalability mentioned above people, implement Web farms. A Web farm is a cluster of Web serves running in parallel. Each Web server
42、 in the cluster has a mirror of your Web site. The traffic of your Web site is equally distributed among the available servers, thus providing load balancing. Storing session variables in the Web server's memory can
43、hamper the Web farm's architecture. Assume that there are three Web servers—S1, S2, and S3—connected in parallel and serving the i</p><p> ASP.NET 2.0 and Session Storage</p><p> ASP.NET 2
44、.0 allows you to store session variables at three distinct locations:</p><p> In the memory of the Web server (in process) </p><p> In the memory of a machine dedicated to storing session vari
45、ables (state server) </p><p> In an SQL Server database </p><p> The first mode is the default. Modes 2 and 3 are often called "out-of-process" modes because the session store is ind
46、ependent of the Web site. In this article, you will restrict yourself to exploring the third mode.</p><p> Storing session variables in the SQL server has the following advantages:</p><p> Sca
47、lability: If you are looking for a highly scalable option to store your session variables, the SQL Server option is for you. It is a much more scalable option than the others. Web farm architecture can very easily access
48、 the session variables because they are stores in an independent database. </p><p> Reliability: Because the data is physically persisted in a database, it is is more reliable than the other options. It has
49、 the ability to survive server restarts. </p><p> Security: SQL Server is more secure than the in-memory or state server option. You can protect your data more easily by configuring SQL Server security. <
50、;/p><p> The session state mode can be configured via a <sessionState> tag of the web.config file.</p><p><b> Notes:</b></p><p> In Web farm scenarios, the applica
51、tion path of the Web site in the IIS metabase should be identical in all the Web servers in the Web farm. </p><p> Session_End event never fires for any of the out-of-process modes. </p><p> C
52、onfiguring SQL Server to Store a Session State</p><p> Before you can actually store a session state in SQL server, you need to configure it. This configuration is done via a command line tool called ASPNET
53、_REGSQL.EXE. You can store the session state in three possible locations within the SQL Server:</p><p> Temporary storage: In this case, the session state is stored in the "tempdb" database of SQL
54、 Server. The tool creates a database called ASPState and adds certain stored procedures for managing session to it. The tool also creates required tables in the "tempdb" database. If you restart the SQL server,
55、 the session data is not persisted. </p><p> Persistent storage: The tool creates a database called ASPState and adds stored procedures for managing a session to it. The session state is stored in the ASPSt
56、ate database. The advantage of this method is that the data is persisted even if you restart the SQL server. </p><p> Custom storage: Both the session state data and the stored procedures are stored in a cu
57、stom database. The database name must be specified in the configuration file. </p><p> The following table lists various command line switches of the tool with respect to session store configuration:</p&
58、gt;<p> Tables and Stored Procedures Created</p><p> As a developer, you possibly will never interact with the session state database directly. However, it would be nice to have a general understand
59、ing of the tables and stored procedures created when you run the ASPNET_REGSQL.EXE tool. Figure 1 shows the tables created by this tool; Figure 2 shows the list of stored procedures.</p><p> Figure 1: Table
60、s created in the SQL Server database</p><p> Figure 2: Stored procedures created in the SQL Server database</p><p> Configuring the Web Site to Store a Session State</p><p> Once
61、 you configure your SQL server to support session state storage, the next step is to configure your Web site. The <sessionState> tag of web.config allows you to specify information about the session store. The foll
62、owing table lists some of the important attributes of this tag: </p><p><b> Example</b></p><p> To illustrate what you've learned up to now, you can develop a simple Web site a
63、nd configure it to store a session state in a SQL Server database.</p><p> Configuring SQL Server</p><p> Open the Visual Studio.NET 2005 command prompt and issue the following command:</p&
64、gt;<p> aspnet_regsql -ssadd -S .\sqlexpress -E -sstype p</p><p> Here, you are using SQL Express as the database with integrated security turned on. Figure 3 shows a sample run of this tool with th
65、e session state type set to "persistent".</p><p> Figure 3: Sample run of ASPNET_REGSQL.EXE tool</p><p> Creating and configuring a Web site</p><p> Now, create a new w
66、eb site and modify the web.config file to have the following markup:</p><p> <sessionState</p><p> mode="SQLServer"</p><p> sqlConnectionString="data source=.\s
67、qlexpress;</p><p> integrated security=true"></p><p> </sessionState></p><p> Here, you added the <sessionState> tag and set its mode attribute to SQLServer.
68、You also specified the sqlConnectionString attribute and pointed it to your SQL Server.</p><p> Creating test web forms</p><p> Add two Web forms in the Web site called Page1.aspx and Page2.as
69、px. The first Web form will display a list of products to choose from. The selected products are stored in a DataSet. The DataSet is then persisted across the requests by storing it in a session variable. The second Web
70、form simply displays the selected products from the DataSet.</p><p> To create the first Web form, follow these steps:</p><p> Drag and drop a GridView control and SQL Data Source control onto
71、 the Web form. </p><p> Configure the SQL Data Source control to select the ProductID, ProductName, and UnitPrice columns from the Products table of the Northwind database. (A detailed explanation of using
72、data source controls is out of scope of this article.) </p><p> Bind the GridView to the SQL Data Source by setting its DataSourceID property to the ID property of the SQL Data Source control </p>&l
73、t;p> From the smart tags of GridView, check the "Allow Selection" checkbox. </p><p> Add a HyperLink control and set its NavigateUrl property to Page2.aspx. </p><p> The followin
74、g markup is generated as a result of above operations (unwanted markup removed):</p><p><b> ...</b></p><p> <asp:GridView ID="GridView1" runat="server"</p
75、><p> AllowPaging="True" AutoGenerateColumns="False"</p><p> CellPadding="4" DataSourceID="SqlDataSource1"</p><p> ForeColor="#333333&qu
76、ot; GridLines="None"</p><p> Width="100%"</p><p> OnSelectedIndexChanged="GridView1_SelectedIndexChanged"</p><p> PageSize="5"></p&g
77、t;<p> <FooterStyle BackColor="#990000" Font-Bold="True"</p><p> ForeColor="White" /></p><p><b> <Columns></b></p><p>
78、; <asp:BoundField DataField="ProductName"</p><p> HeaderText="ProductName"</p><p> SortExpression="ProductName" /></p><p> <asp:BoundField
79、 DataField="UnitPrice"</p><p> HeaderText="UnitPrice"</p><p> SortExpression="UnitPrice" /></p><p> <asp:CommandField SelectText="Add to
80、cart"</p><p> ShowSelectButton="True" /></p><p> </Columns></p><p> </asp:GridView></p><p><b> ...</b></p><p>
81、; <asp:HyperLink ID="HyperLink1" runat="server"</p><p> NavigateUrl="~/Page2.aspx" Font-Bold="True"</p><p> Font-Size="Large">Show my c
82、art</asp:HyperLink></p><p><b> ...</b></p><p> <asp:SqlDataSource ID="SqlDataSource1" runat="server"</p><p> ConnectionString=</p>
83、<p> "<%$ ConnectionStrings:NorthwindConnectionString %>"</p><p> SelectCommand="SELECT [ProductName],</p><p> [UnitPrice] FROM [Products]</p><p> ORD
84、ER BY [ProductName]"></asp:SqlDataSource></p><p> Now, add the following code to the SelectedIndexChanged event of the GridView:</p><p> protected void GridView1_SelectedIndexCha
85、nged(object sender, EventArgs e)</p><p><b> {</b></p><p> DataSet ds=null;</p><p> if (Session["myds"] == null)</p><p><b> {</b>&l
86、t;/p><p> ds = new DataSet();</p><p> DataTable dt = new DataTable();</p><p> dt.Columns.Add(new DataColumn("ProductName"));</p><p> dt.Columns.Add(new DataC
87、olumn("Qty", typeof(System.Int32)));</p><p> ds.Tables.Add(dt);</p><p> Session["myds"] = ds;</p><p><b> }</b></p><p><b> else
88、</b></p><p><b> {</b></p><p> ds = (DataSet)Session["myds"];</p><p><b> }</b></p><p> DataRow row=ds.Tables[0].NewRow();<
89、/p><p> row["productname"]=GridView1.Rows[GridView1.SelectedIndex].</p><p> Cells[0].Text;</p><p> row["Qty"]=1;</p><p> ds.Tables[0].Rows.Add(row)
90、;</p><p><b> }</b></p><p> Here, you create a DataSet and a DataTable. The DataTable contains two DataColumns: ProducyName and Qty. You then store the DataSet in a session variable
91、 called "myds". When the user selects a product, you create a new DataRow, assign the product details to it, and then add this row to the DataTable. Thus, the user selections are persisted across multiple reque
92、sts.</p><p> To create the second Web form, follow these steps:</p><p> Drag and drop a GridView onto the Web form. </p><p> Add two bound fields. </p><p> Set the
93、HeaderText property of the first bound field to "Product Name". Also, set its DataField property to "ProductName". </p><p> Set the HeaderText property of the second bound field to "
94、;Quantity". Also, set its DataField property to "Qty". </p><p> The following markup is created as a result of above operations (unwanted markup removed):</p><p><b> ...&l
95、t;/b></p><p> <asp:GridView ID="GridView1" runat="server"</p><p> AutoGenerateColumns="False" CellPadding="4"</p><p> ForeColor=&quo
96、t;#333333" GridLines="None" Width="100%"></p><p> <FooterStyle BackColor="#1C5E55" Font-Bold="True"</p><p> ForeColor="White" />
97、</p><p><b> <Columns></b></p><p> <asp:BoundField DataField="productname"</p><p> HeaderText="Product Name" /></p><p> &
98、lt;asp:BoundField DataField="qty"</p><p> HeaderText="Quantity" /></p><p> </Columns></p><p> </asp:GridView></p><p><b> ..
99、.</b></p><p> Add the following code in the Page_Load event handler of the second Web form.</p><p> protected void Page_Load(object sender, EventArgs e)</p><p><b> {&
100、lt;/b></p><p> GridView1.DataSource=(DataSet)Session["myds"];</p><p> GridView1.DataBind();</p><p><b> }</b></p><p> Here, you set the Data
101、Source property of the GridView control to your DataSet stored in session. You then bind the GridView by calling its DataBind() method.</p><p> Running the sample Web forms</p><p> To test you
102、r web forms, set Page1.aspx as the start page and run the Web site. You should see something as shown in Figure 4.</p><p> Figure 4: Sample run of Page1.aspx</p><p> Select a few products by c
103、licking the "Add to cart" button. This will add some rows in the DataTable. Recollect that you are storing your DataSet in a session variable. Then, navigate to Page2.aspx by clicking the "Show my cart&quo
104、t; hyperlink. Figure 5 shows Page2.aspx with the previously selected products.</p><p> Figure 5: Sample run of Page2.aspx</p><p> As you can see, Page2.aspx correctly displays the items you se
105、lected on the previous page. This indicates that your session variable was indeed stored in the SQL server database and retrieved on the second page. Also, note that you used the same syntax of storing and retrieving val
106、ues in the session irrespective of the storage mode.</p><p> Disadvantages of Storing the Session State in SQL Server</p><p> Though storing the session state in SQL server makes your Web site
107、 more scalable and reliable, it has some disadvantages of its own:</p><p> Performance: In terms of performance, a SQL Server-based session store is possibly the slowest option. Because your session variabl
108、es are stored in a physical database, it takes more time to get them in and out of the database. This affects the performance of your Web site. </p><p> Cost: Because you are storing your data in a SQL Serv
109、er database, you need to have a SQL Server license. This can add to overall cost of your Web site. </p><p> Serializable data: This method requires that all the data stored in session variables must be seri
110、alizable. This may force you to mark your own classes as [Serializable] if you want to store them in a session. </p><p><b> Summary</b></p><p> ASP.NET 2.0 allows you to store the
111、session state to a SQL Server database. The ASPNET_REGSQL.EXE tool configures the SQL Server to support this feature. Further, the <sessionState> tag configures your Web site to support this mode. Storing a session
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 外文翻譯---開(kāi)發(fā)基于sql server 的cs數(shù)據(jù)庫(kù)應(yīng)用系統(tǒng)
- sql server數(shù)據(jù)庫(kù)教程-
- sql數(shù)據(jù)庫(kù)外文翻譯
- sql server數(shù)據(jù)庫(kù)試卷題庫(kù)
- sql server數(shù)據(jù)庫(kù)日常維護(hù)規(guī)范
- 收縮sql server數(shù)據(jù)庫(kù)日志文件
- sql_server簡(jiǎn)介外文翻譯
- sql server數(shù)據(jù)庫(kù)信息安全實(shí)施策略
- 《sql server數(shù)據(jù)庫(kù)技術(shù)》課程標(biāo)準(zhǔn)
- sql server 2005數(shù)據(jù)庫(kù)課程設(shè)計(jì)
- sql__server數(shù)據(jù)庫(kù)課程設(shè)計(jì)
- sql_server數(shù)據(jù)庫(kù)課程標(biāo)準(zhǔn)
- vb調(diào)用存儲(chǔ)sql server 2000存儲(chǔ)過(guò)程
- XML數(shù)據(jù)在Microsoft SQL Server 2005中的存儲(chǔ)和優(yōu)化.pdf
- SQL SERVER數(shù)據(jù)庫(kù)性能調(diào)整與優(yōu)化.pdf
- sql server觸發(fā)器存入遠(yuǎn)程數(shù)據(jù)庫(kù)
- 基于SQL Server數(shù)據(jù)庫(kù)的PDM系統(tǒng).pdf
- 基于SQL Server數(shù)據(jù)庫(kù)集群的研究.pdf
- 水泥生產(chǎn)過(guò)程數(shù)據(jù)SQL Server存儲(chǔ)方法研究.pdf
- SQL Server數(shù)據(jù)庫(kù)入侵檢測(cè)系統(tǒng)的研究.pdf
評(píng)論
0/150
提交評(píng)論