diff --git a/Bootstrap.DataAccess.SQLite/Role.cs b/Bootstrap.DataAccess.SQLite/Role.cs index 289e92c3..0a6d135f 100644 --- a/Bootstrap.DataAccess.SQLite/Role.cs +++ b/Bootstrap.DataAccess.SQLite/Role.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; -using System.Data.SqlClient; using System.Linq; namespace Bootstrap.DataAccess.SQLite @@ -17,41 +16,30 @@ namespace Bootstrap.DataAccess.SQLite /// /// 保存用户角色关系 /// - /// + /// /// /// - public override bool SaveRolesByUserId(int id, IEnumerable roleIds) + public override bool SaveRolesByUserId(int userId, IEnumerable roleIds) { var ret = false; - DataTable dt = new DataTable(); - dt.Columns.Add("UserID", typeof(int)); - dt.Columns.Add("RoleID", typeof(int)); //判断用户是否选定角色 - roleIds.ToList().ForEach(roleId => dt.Rows.Add(id, roleId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table - string sql = "delete from UserRole where UserID = @UserID;"; + string sql = $"delete from UserRole where UserID = {userId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - if (dt.Rows.Count > 0) + roleIds.ToList().ForEach(rId => { - // insert batch data into config table - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) - { - bulk.DestinationTableName = "UserRole"; - bulk.ColumnMappings.Add("UserID", "UserID"); - bulk.ColumnMappings.Add("RoleID", "RoleID"); - bulk.WriteToServer(dt); - } - } + cmd.CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {userId}, {rId})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(userIds: new List() { id }, roleIds: roleIds); + CacheCleanUtility.ClearCache(userIds: new List() { userId }, roleIds: roleIds); ret = true; } catch (Exception ex) @@ -70,51 +58,64 @@ namespace Bootstrap.DataAccess.SQLite { bool ret = false; var ids = string.Join(",", value); - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles")) + using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; + using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"delete from UserRole where RoleID in ({ids})")) + { + try + { + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from RoleGroup where RoleID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from NavigationRole where RoleID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from Roles where ID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + transaction.CommitTransaction(); + CacheCleanUtility.ClearCache(roleIds: value); + ret = true; + } + catch (Exception ex) + { + transaction.RollbackTransaction(); + throw ex; + } + } } - CacheCleanUtility.ClearCache(roleIds: value); return ret; } /// /// /// - /// + /// /// /// - public override bool SavaRolesByMenuId(int id, IEnumerable roleIds) + public override bool SavaRolesByMenuId(int menuId, IEnumerable roleIds) { var ret = false; - DataTable dt = new DataTable(); - dt.Columns.Add("NavigationID", typeof(int)); - dt.Columns.Add("RoleID", typeof(int)); //判断用户是否选定角色 - roleIds.ToList().ForEach(roleId => dt.Rows.Add(id, roleId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete role from config table - string sql = "delete from NavigationRole where NavigationID=@NavigationID;"; + string sql = $"delete from NavigationRole where NavigationID = {menuId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@NavigationID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - // insert batch data into config table - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + roleIds.ToList().ForEach(rId => { - bulk.BatchSize = 1000; - bulk.DestinationTableName = "NavigationRole"; - bulk.ColumnMappings.Add("NavigationID", "NavigationID"); - bulk.ColumnMappings.Add("RoleID", "RoleID"); - bulk.WriteToServer(dt); - transaction.CommitTransaction(); - } + cmd.CommandText = $"Insert Into NavigationRole (NavigationID, RoleID) Values ( {menuId}, {rId})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + }); + transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { id }); + CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { menuId }); ret = true; } catch (Exception ex) @@ -128,40 +129,32 @@ namespace Bootstrap.DataAccess.SQLite /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// - /// + /// /// /// - public override bool SaveRolesByGroupId(int id, IEnumerable roleIds) + public override bool SaveRolesByGroupId(int groupId, IEnumerable roleIds) { var ret = false; //构造表格 - DataTable dt = new DataTable(); - dt.Columns.Add("RoleID", typeof(int)); - dt.Columns.Add("GroupID", typeof(int)); - roleIds.ToList().ForEach(roleId => dt.Rows.Add(roleId, id)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table - string sql = "delete from RoleGroup where GroupID=@GroupID"; + string sql = $"delete from RoleGroup where GroupID = {groupId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + roleIds.ToList().ForEach(rId => { - bulk.BatchSize = 1000; - bulk.DestinationTableName = "RoleGroup"; - bulk.ColumnMappings.Add("RoleID", "RoleID"); - bulk.ColumnMappings.Add("GroupID", "GroupID"); - bulk.WriteToServer(dt); - transaction.CommitTransaction(); - } + cmd.CommandText = $"Insert Into RoleGroup (GroupID, RoleID) Values ( {groupId}, {rId})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + }); + transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { id }); + CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { groupId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Role.cs b/Bootstrap.DataAccess/Role.cs index d64b5cef..c392e713 100644 --- a/Bootstrap.DataAccess/Role.cs +++ b/Bootstrap.DataAccess/Role.cs @@ -69,26 +69,25 @@ namespace Bootstrap.DataAccess /// /// 保存用户角色关系 /// - /// + /// /// /// - public virtual bool SaveRolesByUserId(int id, IEnumerable roleIds) + public virtual bool SaveRolesByUserId(int userId, IEnumerable roleIds) { var ret = false; DataTable dt = new DataTable(); dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("RoleID", typeof(int)); //判断用户是否选定角色 - roleIds.ToList().ForEach(roleId => dt.Rows.Add(id, roleId)); + roleIds.ToList().ForEach(roleId => dt.Rows.Add(userId, roleId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table - string sql = "delete from UserRole where UserID = @UserID;"; + string sql = $"delete from UserRole where UserID = {userId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); if (dt.Rows.Count > 0) { @@ -103,7 +102,7 @@ namespace Bootstrap.DataAccess } transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(userIds: new List() { id }, roleIds: roleIds); + CacheCleanUtility.ClearCache(userIds: new List() { userId }, roleIds: roleIds); ret = true; } catch (Exception ex) @@ -216,26 +215,25 @@ namespace Bootstrap.DataAccess /// /// /// - /// + /// /// /// - public virtual bool SavaRolesByMenuId(int id, IEnumerable roleIds) + public virtual bool SavaRolesByMenuId(int menuId, IEnumerable roleIds) { var ret = false; DataTable dt = new DataTable(); dt.Columns.Add("NavigationID", typeof(int)); dt.Columns.Add("RoleID", typeof(int)); //判断用户是否选定角色 - roleIds.ToList().ForEach(roleId => dt.Rows.Add(id, roleId)); + roleIds.ToList().ForEach(roleId => dt.Rows.Add(menuId, roleId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete role from config table - string sql = "delete from NavigationRole where NavigationID=@NavigationID;"; + string sql = $"delete from NavigationRole where NavigationID = {menuId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@NavigationID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table @@ -249,7 +247,7 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { id }); + CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { menuId }); ret = true; } catch (Exception ex) @@ -294,26 +292,25 @@ namespace Bootstrap.DataAccess /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// - /// + /// /// /// - public virtual bool SaveRolesByGroupId(int id, IEnumerable roleIds) + public virtual bool SaveRolesByGroupId(int groupId, IEnumerable roleIds) { var ret = false; //构造表格 DataTable dt = new DataTable(); dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("GroupID", typeof(int)); - roleIds.ToList().ForEach(roleId => dt.Rows.Add(roleId, id)); + roleIds.ToList().ForEach(roleId => dt.Rows.Add(roleId, groupId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table - string sql = "delete from RoleGroup where GroupID=@GroupID"; + string sql = $"delete from RoleGroup where GroupID = {groupId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table @@ -327,7 +324,7 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { id }); + CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { groupId }); ret = true; } catch (Exception ex)