vb.net机房收费系统重构——存储过程的使用
在敲寫七層登陸過程中,接觸了存儲過程的概念,但當時還沒使用。在正式的重構過程中,一點點添加,將存儲過程也添加到了重構中。才正式了解了存儲過程。
首先
【存儲過程的概念】
存儲過程Procedure是一組為了完成特定功能的SQL語句集合,經編譯后存儲在數據庫中,用戶通過指定存儲過程的名稱并給出參數來執行。
存儲過程中可以包含邏輯控制語句和數據操縱語句,它可以接受參數、輸出參數、返回單個或多個結果集以及返回值。
【存儲過程的使用】
首先,打開數據庫——存儲過程
右鍵—新建存儲過程
?
? CREATE PROCEDURE <存儲過程名稱> -- Add the parameters for the stored procedure here<@Param1, sysname, @p1> <datatype_for_param1, int=""> = <default_value_for_param1, 0="">, <@Param2, sysname, @p2> <datatype_for_param2, int=""> = <default_value_for_param2, 0=""> AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;-- Insert statements for procedure hereSELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2> END GO</default_value_for_param2,></datatype_for_param2,></default_value_for_param1,></datatype_for_param1,>?
例如在機房收費系統中注冊新用戶的存儲過程
?
CREATE PROCEDURE [dbo].[pro_Register]-- Add the parameters for the stored procedure here@Cardno char(12),@Studentno char(12),@Studentname char(12),@Sex char(12),@Department char(12),@Grade char(12),@Class char(12),@Explain varchar(50),@IsCheck char(12),@Type char(12),@Cash numeric(18,1),@Date date,@Time time(0),@UserID char(12),@Status char(12)AS BEGINinsert into T_Student(Cardno,Studentname,Studentno,Sex,Department,Grade,Class,Cash,Explain,IsCheck,UserID,Type,Status)values(@Cardno ,@Studentname,'12',@Sex,@Department,@Grade,@Class,@Cash,@Explain,@IsCheck,@UserID,@Type,@Status)insert into T_Recharge(Cardno,Studentno,Addmoney,Date,Time,UserID,Status)values(@Cardno,@Studentno,@Cash,@Date,@Time,@UserID,@Status)END?
?
?
存儲過程名稱為:pro_Register
調用時在D層
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | Public Function SelectStudent(ByVal student As Entity.StudentEntity, ByVal recharge As Entity.RechargeEntity) As Boolean Implements IDAL.IStudent.SelectStudent ????????Dim sqlparams As SqlParameter() = {New SqlParameter("@Cardno", student.Cardno), ???????????????????????????????????????????New SqlParameter("@Studentname", student.StudentName), ???????????????????????????????????????????New SqlParameter("@Studentno", student.Studentno), ???????????????????????????????????????????New SqlParameter("@Sex", student.Sex), ???????????????????????????????????????????New SqlParameter("@Department", student.Department), ???????????????????????????????????????????New SqlParameter("@Grade", student.Status), ???????????????????????????????????????????New SqlParameter("@Class", student.Clas), ???????????????????????????????????????????New SqlParameter("@Cash", student.Cash), ???????????????????????????????????????????New SqlParameter("@Explain", student.Explain), ???????????????????????????????????????????New SqlParameter("@IsCheck", "未結賬"), ???????????????????????????????????????????New SqlParameter("@UserID", student.UserID), ???????????????????????????????????????????New SqlParameter("@Type", student.Type), ???????????????????????????????????????????New SqlParameter("@Status", student.Status), ???????????????????????????????????????????New SqlParameter("@Date", recharge.Dater), ???????????????????????????????????????????New SqlParameter("@Time", recharge.Timer) ??????????????????????????????????????????} ????????Dim strSql = "<span style="color:#FF0000;">pro_Register</span>" ????????Dim helper As New SqlHelper ????????Dim dt As New Integer ????????dt = helper.ExeAddDelUpdate("pro_Register", CommandType.StoredProcedure, sqlparams) ????????If dt > 0 Then ????????????Return True ????????Else ????????????Return False ????????End If |
?
【存儲過程的優點】
存儲過程允許標準組件式編程
1、存儲過程允許標準式組件編程
存儲過程創建后可以在程序中被多次調用執行,而不必重新編寫該存儲過程的SQL語句。而且數據庫專業人員可以隨時對存儲過程進行修改,但對應用程序源代碼卻毫無影響,從而極大的提高了程序的可移植性。
2、 存儲過程能夠實現較快的執行速度
如果某一操作包含大量的T-SQL語句代碼,分別被多次執行,那么存儲過程要比批處理的執行速度快得多。因為存儲過程是預編譯的,在首次運行一個存儲過程時,查詢優化器對其進行分析、優化,并給出最終被存在系統表中的存儲計劃。而批處理的T-SQL語句每次運行都需要預編譯和優化,所以速度就要慢一些。
在機房收費系統中,由于數據量和用戶都比較小,可能體會不到批處理和存儲過程的區別。當用戶較多數據量較大時,此方法的優點較為明顯。
3、 存儲過程減輕網絡流量
對于同一個針對數據庫對象的操作,如果這一操作所涉及到的T-SQL語句被組織成一存儲過程,那么當在客戶機上調用該存儲過程時,網絡中傳遞的只是該調用語句,否則將會是多條SQL語句。從而減輕了網絡流量,降低了網絡負載。
此優點也是當數據量較大時可以明顯體會出存儲過程的優勢。
4、 存儲過程可被作為一種安全機制來充分利用
系統管理員可以對執行的某一個存儲過程進行權限限制,從而能夠實現對某些數據訪問的限制,避免非授權用戶對數據的訪問,保證數據的安全。
總結
以上是生活随笔為你收集整理的vb.net机房收费系统重构——存储过程的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机房收费系统中——存储过程中加入事务,实
- 下一篇: 界面原型创建工具Axure 基本操作