機房收費系統--預存程序,收費系統預存程序

來源:互聯網
上載者:User

機房收費系統--預存程序,收費系統預存程序

在這次機房收費系統中學習到的東西還真不少。E-R圖,視圖,預存程序,觸發器等等。這些東西都是在之前學習過的,這次運用到實踐中,收穫倍增。

運用預存程序的好處

1、預存程序只在創造時進行編譯,以後每次執行預存程序都不需再重新編譯,而一般SQL語句每執行一次就編譯一次,所以使用預存程序可提高資料庫執行速度。

2、當對資料庫進行複雜操作時(如對多個表進行Update,Insert,Query,Delete時),可將此複雜操作用預存程序封裝起來與資料庫提供的交易處理結合一起使用。

3、過程可以重複使用,可減少資料庫開發人員的工作量

4、全性高,可設定只有某些使用者才具有對指定預存程序的使用權。

預存程序的實際應用

在退卡中我用到了預存程序,下面就以退卡為例。

建立一個預存程序

<span style="font-family:KaiTi_GB2312;font-size:18px;">create  procedure proc_CloseCard  --建立預存程序@cardId varchar(10),@closeCardUserId varchar(10) --定義參數asbegin--在退卡表中插入資料insert into t_CloseCard(cardId,closeCash,registryDate,registryTime,isChecked,registryUserId) select cardId ,balance ,date,time, isChecked ,userId  from t_card where cardId =@cardId--將卡表中的相關卡刪除delete from t_card where cardId=@cardId--對退卡表進行更新update t_closecard set closeuserId =@closeCardUserId  where cardId =@cardId end</span>

退卡D層對預存程序的調用

<span style="font-family:KaiTi_GB2312;font-size:18px;">' <summary>    ' 添加退卡資訊    ' </summary>    ' <param name="_enCloseCardEntity">退卡實體類</param>    '<returns>插入成功返回True,失敗返回False</returns>    Public Function AddCloseCard(ByVal enCloseCardEntity As CloseCardEntity) As Boolean        Dim sqlHelper As New SQLHelper '執行個體化SQLHelper        'SQL語句        Dim cmdText As String = "proc_CloseCard"        '定義命令類型,預存程序        Dim cmdType As CommandType        cmdType = CommandType.StoredProcedure        '定義參數數組        Dim sqlParams As SqlParameter()        '給參數數組賦值        sqlParams = {New SqlParameter("@cardId", enCloseCardEntity.cardId),                     New SqlParameter("@closeCardUserId", SharedUserId.userId)}        Return sqlHelper.ExecuteNoQuery(cmdText, cmdType, sqlParams)    End Function</span>
用預存程序進行操作的結果和用SQL語句的結果是一樣的。只是一個SQL語句寫在了資料庫端,一個寫在了編譯端。

用過以後就會發現預存程序其實挺簡單的。但是聽師傅說預存程序是面向過程的,不符合物件導向的思想。以後還是少用。但是現在處於學習階段,用用也是好的,最起碼要會用。在實踐中不斷進步。






 

相關文章

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.