#52006/3/29 11:18:18
output是用参数输出的,一般存储过程用法比较多
下面是个例子
存储过程:
CREATE PROCEDURE sp_AccountRole_Create
@CategoryID int,
@RoleName nvarchar(10),
@Description nvarchar(50),
@RoleID int output
AS
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
建立数据记录的函数
public int Create(RoleInfo role)
{
int rowsAffected = 0;
SqlParameter[] parameters =
{
new SqlParameter("@CategoryID", SqlDbType.Int, 4),
new SqlParameter("@RoleName", SqlDbType.NVarChar, 10),
new SqlParameter("@Description", SqlDbType.NVarChar, 50),
new SqlParameter("@RoleID", SqlDbType.Int, 4)
};
parameters[0].value = role.CategoryID;
parameters[1].value = role.RoleName;
parameters[2].value = role.Description;
parameters[3].Direction = ParameterDirection.Output;
try
{
RunCommand("sp_AccountRole_Create", parameters, out rowsAffected);
}
catch
{
throw ( new AppException("在建立角色时发生意外错误。") );
}
if (rowsAffected <= 0)
{
throw ( new AppException("已经存在名为" + role.RoleName + "的角色名称,请重新再选其它名称。") );
}
else
{
return (int)parameters[3].value;
}
}
RunCommand是包装过的函数,就是执行一下存储过程,红色二句是用于输出ID号的
编辑历史:[此帖最近一次被 蓝鲸 编辑过(编辑时间:2006-03-29 11:26:13)]
非常大鱼