存储过程之实践期

         前一篇博客通过存储过程的基础知识来展开,接下来通过具体的实践来实现它的具体步骤:

         以合作机房收费系统的注册为例(需求:在注册中需要涉及到三张表:卡表,学生表及充值表)为了减少工程的代码量和复杂度就在数据库中建一个存储过程来搞定,这样在前台代码中就会很省力,不用来回的调用好多方法。

         一、在数据库中创建存储过程

CREATE PROCEDURE [dbo].[PROC_StudentRegister]
@studentID varchar(20) ,@Name varchar(20),@sex varchar(20),@department varchar(20),@grade varchar(20),@Classroom varchar(20),@explain varchar(20),@userID varchar(20),
@recharge money,@rechargeDate date,@rechargeTime time(7),@isCheck varchar(20),
@cardID varchar(20),@Balance money,@registerDate date,@registerTime time(7),@cardFix varchar(20),@state varchar(20)
AS  
Begin
	
	--添加学生表
	insert into T_StudentInfo(studentID ,Name,sex,department,grade,Classroom,explain,userID) values (@studentID ,@Name,@sex,@department,@grade,@Classroom,@explain,@userID)  

	--添加卡表
	insert into T_CardInfo(cardID,Balance,registerDate,registerTime,cardFix,state,studentID,isCheck,userID) values(@cardID,@Balance,@registerDate,@registerTime,@cardFix,@state,@studentID,@isCheck,@userID)    
 
	--添加充值表
	insert into T_RechargeInfo(cardID,recharge,rechargeDate,rechargeTime,userID,isCheck) values(@cardID,@recharge,@rechargeDate,@rechargeTime,@userID,@isCheck)  
End

         二、代码中调用存储过程

 ''' <summary>    ''' 添加卡接口,注册时添加卡,插入卡表T_CardInfo,学生表,充值表
    ''' state,studentID,isCheck,userID依次对应实体CardInfoEntity中字段。
    ''' </summary>
    ''' <param name="enCard"></param>
    Public Function RegisterCardNo(ByVal enCard As CardInfoEntity, ByVal enStudent As StudentInfoEntity, ByVal enRecharge As RechargeInfoEntity) As Boolean Implements ISQLCardInfoDAL.RegisterCardNo
        Dim strSql As String
        Dim bln As Boolean
        Dim sqlParames As SqlParameter()
        '给参数赋值
        sqlParames = New SqlParameter() {
          New SqlParameter("@cardID", enCard.strCardID),
          New SqlParameter("@balance", enCard.dblBalance),
          New SqlParameter("@registerDate", enCard.strRegisterDate),
          New SqlParameter("@registerTime", enCard.strRegisterTime),
          New SqlParameter("@cardFix", enCard.strCardFix),
          New SqlParameter("@state", enCard.strState),
          New SqlParameter("@studentID", enCard.strStudentID),
          New SqlParameter("@isCheck", enCard.strIsCheck),
          New SqlParameter("@userID", enCard.strUserID),
          New SqlParameter("@name", enStudent.strName),
          New SqlParameter("@sex", enStudent.strSex),
          New SqlParameter("@department", enStudent.strDepartment),
          New SqlParameter("@grade", enStudent.strGrade),
          New SqlParameter("@classroom", enStudent.strClassroom),
          New SqlParameter("@explain", enStudent.strExplain),
          New SqlParameter("@recharge", enRecharge.dblRecharge),
          New SqlParameter("@rechargeDate", enRecharge.strRechargeDate),
          New SqlParameter("@rechargeTime", enRecharge.strRechargeTime)
        }
        '调用存储过程的SQL语句
        strSql = "PROC_StudentRegister"
        '执行存储过程
        bln = sqlHelper.ExecuteNoQuery(sqlParames, CommandType.StoredProcedure, strSql)
        Return bln
    End Function
           综上所述就是存储过程的创建及执行的过程。只有当你在实践中运用了才会真正的理解,这就是所谓的实践出真知。希望在今后的学习中不断地实践,有不对的在今后的学习中慢慢积累!加油


你可能感兴趣的:(存储过程之实践期)