导航

带输出参数的存储过程

Posted on 2009-10-19 22:31  kingwangzhen  阅读(570)  评论(0编辑  收藏  举报

USE [Northwind]
GO
/****** 对象:  StoredProcedure [dbo].[Users_Insert]    脚本日期: 10/19/2009 22:26:16 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Users_Insert]
(

 @UUserId int    OUTPUT,

 @UUserName nvarchar (30)  ,

 @UPassWord nvarchar (50) 
)
AS    
    INSERT INTO [dbo].[Users]
     (
     [U_UserName]
     ,[U_PassWord]
     )
    VALUES
     (
     @UUserName
     ,@UPassWord
     )
    
    -- Get the identity value
    SET @UUserId = SCOPE_IDENTITY()

============================================================

declare @id int;
exec Users_Insert @id output, 'aaa' ,'aaa'
select
 @id;       

===========================================================

 

using (SqlCommand cmd=new SqlCommand("proc_userinfo",con))
14        {
15          //什么作用不记得了只记得调用存储过程该语句不能少
16          cmd.CommandType = CommandType.StoredProcedure;
17          //添加存储过程输入输出参数类型及输入参数值
18          cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value=username;
19          cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value=pwd;
20          cmd.Parameters.Add("@grade", SqlDbType.VarChar, 50).Value=grade;
21          cmd.Parameters.Add("@count", SqlDbType.Int);
22          cmd.Parameters["@username"].DbType = DbType.String;
23          //指定参数类型是输入参数还是输出参数
24          cmd.Parameters["@username"].Direction = ParameterDirection.Input;
25          cmd.Parameters["@pwd"].DbType = DbType.String;
26          //指定参数类型是输入参数还是输出参数
27          cmd.Parameters["@pwd"].Direction = ParameterDirection.Input;
28          cmd.Parameters["@grade"].DbType = DbType.String;
29          //指定参数类型是输入参数还是输出参数
30          cmd.Parameters["@grade"].Direction = ParameterDirection.Input;
31          cmd.Parameters["@count"].DbType = DbType.Int32;
32          //指定参数类型是输入参数还是输出参数
33          cmd.Parameters["@count"].Direction = ParameterDirection.Output;
34          //执行存储过程 此处类似于查询语句
35          cmd.ExecuteScalar();
36          //接受执行存储过程后的返回值
37          n = (int)cmd.Parameters["@count"].Value;