级联删除(使用存储过程)功能完成

This commit is contained in:
liuchun_0206@163.com 2016-11-07 15:17:10 +08:00
parent 61c49e4609
commit 3135ff24f0
4 changed files with 113 additions and 52 deletions

View File

@ -59,29 +59,23 @@ namespace Bootstrap.DataAccess
/// <param name="ids"></param> /// <param name="ids"></param>
public static bool DeleteGroup(string ids) public static bool DeleteGroup(string ids)
{ {
var ret = false; bool ret = false;
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret; if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) try
{ {
try using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups"))
{ {
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Groups where ID in ({0})", ids); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids, ParameterDirection.Input));
sql += string.Format("delete from RoleGroup where GroupID in ({0});", ids); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
sql += string.Format("delete from UserGroup where GroupID in ({0});", ids);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(groupIds: ids);
ret = true;
} }
catch (Exception ex) CacheCleanUtility.ClearCache(groupIds: ids);
{ ret = true;
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}
return ret;
} }
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
return ret;
} }
/// <summary> /// <summary>
/// 保存新建/更新的群组信息 /// 保存新建/更新的群组信息

View File

@ -130,24 +130,19 @@ namespace Bootstrap.DataAccess
{ {
bool ret = false; bool ret = false;
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret; if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) try
{ {
try using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus"))
{ {
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Navigations where ID in ({0})", ids); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids, ParameterDirection.Input));
sql += string.Format("delete from NavigationRole where NavigationID in ({0});", ids); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(menuIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
} }
CacheCleanUtility.ClearCache(menuIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
} }
return ret; return ret;
} }

View File

@ -141,30 +141,23 @@ namespace Bootstrap.DataAccess
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
/// <param name="IDs"></param> /// <param name="IDs"></param>
public static bool DeleteRole(string IDs) public static bool DeleteRole(string ids)
{ {
bool ret = false; bool ret = false;
if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret; if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) try
{ {
try using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles"))
{ {
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0});", IDs); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids, ParameterDirection.Input));
sql += string.Format("delete from UserRole where RoleID in ({0});", IDs); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
sql += string.Format("delete from RoleGroup where RoleID in ({0});", IDs);
sql += string.Format("delete from NavigationRole where RoleID in ({0});", IDs);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(roleIds: IDs);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
} }
CacheCleanUtility.ClearCache(roleIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
} }
return ret; return ret;
} }

View File

@ -27,3 +27,82 @@ BEGIN
exec(@sql) exec(@sql)
END END
GO GO
Drop PROCEDURE Proc_DeleteRoles
GO
-- =============================================
-- Author: LiuChun
-- Create date: 2016-11-07
-- Description:
-- =============================================
Create PROCEDURE Proc_DeleteRoles
-- Add the parameters for the stored procedure here
@ids varchar(max)
WITH ENCRYPTION
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- Insert statements for procedure here
declare @sql varchar(max)
set @sql = 'delete from UserRole where RoleID in (' + @ids + ');'
set @sql += 'delete from RoleGroup where RoleID in (' + @ids + ');'
set @sql += 'delete from NavigationRole where RoleID in (' + @ids + ');'
set @sql += 'delete from Roles where ID in (' + @ids + ');'
exec(@sql)
END
GO
Drop PROCEDURE Proc_DeleteGroups
GO
-- =============================================
-- Author: LiuChun
-- Create date: 2016-11-07
-- Description:
-- =============================================
Create PROCEDURE Proc_DeleteGroups
-- Add the parameters for the stored procedure here
@ids varchar(max)
WITH ENCRYPTION
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- Insert statements for procedure here
declare @sql varchar(max)
set @sql = 'delete from UserGroup where GroupID in (' + @ids + ');'
set @sql += 'delete from RoleGroup where GroupID in (' + @ids + ');'
set @sql += 'delete from Groups where ID in (' + @ids + ');'
exec(@sql)
END
GO
Drop PROCEDURE Proc_DeleteMenus
GO
-- =============================================
-- Author: LiuChun
-- Create date: 2016-11-07
-- Description:
-- =============================================
Create PROCEDURE Proc_DeleteMenus
-- Add the parameters for the stored procedure here
@ids varchar(max)
WITH ENCRYPTION
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- Insert statements for procedure here
declare @sql varchar(max)
set @sql = 'delete from NavigationRole where NavigationID in (' + @ids + ');'
set @sql += 'delete from Navigations where ID in (' + @ids + ');'
exec(@sql)
END
GO