From b1d7e5bf59dee706c110bd5e19dd3bb6ab566169 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Tue, 23 Oct 2018 14:41:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=EF=BC=9A=E5=AE=8C?= =?UTF-8?q?=E5=96=84SQLite=E7=BB=84=E4=BB=B6=E4=B8=8BRole=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E6=96=B9=E6=B3=95=EF=BC=8C=E7=A7=BB=E9=99=A4=E5=AF=B9?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E8=BF=87=E7=A8=8B=E5=92=8CSqlBulkCopy?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess.SQLite/Role.cs | 111 +++++++++++++--------------- Bootstrap.DataAccess/Role.cs | 33 ++++----- 2 files changed, 67 insertions(+), 77 deletions(-) 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)