存储过程:
存储过程(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
存储过程的优点:
总结:使用存储过程,在一定程度上减少了代码量,又尝试使用不曾用过得东西,会有成就感。
原文:http://blog.csdn.net/u010924845/article/details/38589341