存储过程:
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完毕特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出參数(假设该存储过程带有參数)来运行它。存储过程是数据库中的一个重要对象,不论什么一个设计良好的数据库应用程序都应该用到存储过程。
存储过程的建立:
选中存储过程,右击——新建存储过程,则出现以下的代码。
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> -- 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 here SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2> END GO
建好的存储过程:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[PROC_SettleAccount] -- Add the parameters for the stored procedure here @Recharge numeric(18,2) ,@ReturnM numeric(18,2), @Income numeric(18,2),@UserName char(10), @SetDate char(10),@SetTime char(10) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here insert into Bill (Recharge ,ReturnM ,Income ,UserName ,SetDate ,SetTime ) values (@Recharge ,@ReturnM ,@Income ,@UserName ,@SetDate ,@SetTime ) update RechargeRecords set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账' update ReturnMoneyRecords set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账' update Cards set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账' END
存储过程的使用:
Public Function SettleAccount(bill As Entity.Bill) As Boolean Implements IDAL.IBill.SettleAccount Dim cmdtext As String cmdtext = "PROC_SettleAccount" '用存储过程的名称来替换SQL语句 bill.SetDate = Format(Now, "yyyy-MM-dd") '获得当前日期 bill.SetTime = Format(Now, "HH:mm:ss") '获得当前时间 '加入參数 Dim sqlparameter As SqlParameter() = {New SqlParameter("@Recharge", bill.Recharge), New SqlParameter("@ReturnM", bill.ReturnM), New SqlParameter("@Income", bill.Income), New SqlParameter("@UserName", bill.UserName), New SqlParameter("@SetDate", bill.SetDate), New SqlParameter("@SetTime", bill.SetTime)} Dim helper As New SqlHelper Dim flag As Boolean '中间的參数变为存储过程特用的參数 flag = helper.ExecAddDelUpdate(cmdtext, CommandType.StoredProcedure, sqlparameter) Return flag End Function
仅仅要将存储过程的名字替换SQL的语句。在运行的时候。也要换成存储过程特用的參数。
存储过程的长处:
1.反复使用。
存储过程能够反复使用。从而能够降低数据库开发者的工作量。
2.提高性能。存储过程在创建的时候在进行了编译,将来使用的时候不再又一次翻译。一般的SQL语句每运行一次就须要编译一次,所以使用存储过程提高了效率。
3.降低网络流量。
存储过程位于server上,调用的时候仅仅须要传递存储过程的名称以及參数就能够了,因此降低了网络传输的数据量。
4.安全性。參数化的存储过程能够防止SQL注入式攻击,并且能够将Grant、Deny以及Revoke权限应用于存储过程。
详见百度百科
总结:使用存储过程,在一定程度上降低了代码量。又尝试使用不曾用过得东西。会有成就感。