存储过程初探
为了修改别人的代码,因为涉及到存储过程,因为专门抽时间学习了一下存储过程。
1、先写一个存储过程:
在Sql server 中新建存储过程,会出现一个存储过程的模板,稍加修改如下(该存储过程查询一个人):
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,xiajf,Name>
-- Create date: <Create Date,2011-4-11,>
-- Description: <Description,for test,>
-- =============================================
CREATE PROCEDURE [dbo].[usp_test]
-- Add the parameters for the stored procedure here
@UserName nvarchar (50)
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 UserName
FROM dbo.OrganizationUsers
WHERE UserName = @UserName
END
GO
2、在Sql Server中调用存储过程:
执行上述存储过程,然后用一下代码调用(张三作为参数传到上述存储过程中):
USE ynpdeicp; GO EXECUTE usp_test @userName = N'张三' ; GO
3、在C#代码中调用存储过程(db是一个连接的数据库实例):
db.ExecuteScalar("usp_test", "张三")