一个简单的存储过程

CREATE PROCEDURE p_out
@a as int,
@b as int,
@c as int output
AS
set @c=@a+@b
return '10'
---------------------
.........
SqlCommand cmd2=new SqlCommand("p_out",conn);
cmd2.CommandType=CommandType.StoredProcedure;
cmd2.Parameters.Add("@a",SqlDbType.Int);
cmd2.Parameters.Add("@b",SqlDbType.Int);
SqlParameter para_c=cmd2.Parameters.Add("@c",SqlDbType.Int);
SqlParameter para_r=cmd2.Parameters.Add("returnvalue",SqlDbType.Int);
cmd2.Parameters[0].Value=2;
cmd2.Parameters[1].Value=3;
para_c.Direction=ParameterDirection.Output;
para_r.Direction=ParameterDirection.ReturnValue;
conn.open();
cmd2.ExecuteNonQuery();
conn.close();
this.Response.Write(para_c.value.ToString());
this.Response.Write(para_r.value.ToString());

---------------------------------------------------------------------------------------------------

CREATE PROCEDURE sp_AccountRole_Create
@CategoryID int,
@RoleName nvarchar(10),
@Description nvarchar(50),
@RoleID int outputAS   
DECLARE @Count int
SELECT @Count = Count(RoleID) FROM Account_Role WHERE RoleName = @RoleName
IF @Count = 0
INSERT INTO Account_Role (CategoryID, RoleName, Description) values
(@CategoryID, @RoleName, @Description) SET @RoleID = @@IDENTITY
RETURN 1
GO

执行存储过程的C#过程:
SqlConnection DbConnection = new SqlConnection(mConnectionString);
SqlCommand command = new SqlCommand( "sp_AccountRole_Create", DbConnection );
DbConnection.Open(connectString);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);
command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);
command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
command.Parameters.Add("@RoleID", SqlDbType.Int, 4);
command.Parameters.Add("Returnvalue", SqlDbType.Int, 4, ParameterDirection.Returnvalue,false, 0,0,

string.Empty,DataRowVersion.Default, null);
command.parameters["@CategoryID"].value = permission.CategoryID;
command.parameters["@RoleName"].value = permission.PermissionName;
command.parameters["@Description"].value = permission.Description;
command.parameters["@RoleID"].Direction = ParameterDirection.Output;
int rowsAffected = command.ExecuteNonQuery();
int result = command.parameters["Returnvalue"].value;
int newID = command.parameters["@RoleID"].value;

 

posted @ 2008-07-14 15:19  洗碗心得  阅读(231)  评论(0编辑  收藏  举报