存储过程:
存储过程(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.减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。
4.安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。
详见百度百科
总结:使用存储过程,在一定程度上减少了代码量,又尝试使用不曾用过得东西,会有成就感。