开发技巧:您不能删除别人的数据!
作者:C/S框架网  发布日期:2018-06-05 19:27:09
  开发技巧:您不能删除别人的数据!


解决方案:重写DAL层的Delete方法,调用存储过程删除记录。

删除业务逻辑:
1.检查数据拥有者是否当前用户
2.统计关键业务数据使用客户资料次数,>0不可删除!
3.添加删除日志

C# Code:

public override bool Delete(string keyValue)
{
   CommandHelper cmd = _Database.CreateSqlProc("usp_DeleteCustomer");
   cmd.AddParam("@Key", DbType.String, keyValue);
   cmd.AddParam("@User", DbType.String, _Loginer.Account);
   int i = _Database.ExecuteCommand(cmd.Command);
   return i != 0;
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



SQL Store Procedure:

ALTER PROCEDURE usp_DeleteCustomer
@Key VARCHAR(50),--客户编码
@User VARCHAR(20) --当前用户
AS
BEGIN

/******************************************************
程序说明:删除客户资料
-------------------------------------------------------
SELECT * FROM dbo.dt_Customer
SELECT * FROM NN_System.dbo.sys_LogOperation

SELECT * FROM dbo.tb_QO
-------------------------------------------------------
usp_DeleteCustomer 'M005211','admin'
usp_DeleteCustomer 'M005811','maggie'
*******************************************************/

DECLARE @MSG NVARCHAR(200)

--检查数据拥有者是否当前用户
IF @User<>(SELECT ISNULL(CreatedBy,'') FROM dbo.dt_Customer WHERE CustomerCode=@Key)
BEGIN
SELECT @MSG='您不能删除别人的数据!';
RAISERROR (@MSG,16,1);
RETURN;
END;

--统计关键业务数据使用次数
DECLARE @Count INT
SELECT @Count=
(SELECT COUNT(1) FROM tb_QO WHERE CustomerCode=@Key)+
(SELECT COUNT(1) FROM tb_PI WHERE CustomerCode=@Key)+
(SELECT COUNT(1) FROM tb_IV WHERE CustomerCode=@Key)+
(SELECT COUNT(1) FROM tb_PM WHERE CustomerCode=@Key)

IF (@Count>0)
BEGIN
SELECT @MSG='您不能删除当前资料,有'+CAST(@Count AS VARCHAR)+'个业务功能在使用!';
RAISERROR (@MSG,16,1);
RETURN;
END;

--删除数据
DELETE dt_Customer WHERE CustomerCode=@Key

--添加删除日志
INSERT INTO NN_System.dbo.sys_LogOperation(OperationType,OperationTime,Account,MSG)
SELECT 'Delete',GETDATE(),@User,'删除记录,PK:'+@Key

END;

//来源:C/S框架网(www.csframework.com) QQ:23404761






上一篇 下一篇