From 52427c77b2aea4ebdbc40f3478f15f1eea6dcf53 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Sat, 8 Sep 2018 19:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E9=99=A4Try/Catch=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=93=8D=E4=BD=9C=E4=BD=BF=E7=94=A8=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E8=A1=8C=E6=95=B0=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess/DictHelper.cs | 73 +++++---------- Bootstrap.DataAccess/GroupHelper.cs | 101 +++++++------------- Bootstrap.DataAccess/LogHelper.cs | 69 +++++--------- Bootstrap.DataAccess/MenuHelper.cs | 75 ++++++--------- Bootstrap.DataAccess/MessageHelper.cs | 41 ++++---- Bootstrap.DataAccess/RoleHelper.cs | 129 ++++++++++---------------- Bootstrap.DataAccess/TaskHelper.cs | 29 +++--- 7 files changed, 187 insertions(+), 330 deletions(-) diff --git a/Bootstrap.DataAccess/DictHelper.cs b/Bootstrap.DataAccess/DictHelper.cs index 2a8ac49d..7a55c2c8 100644 --- a/Bootstrap.DataAccess/DictHelper.cs +++ b/Bootstrap.DataAccess/DictHelper.cs @@ -1,6 +1,5 @@ using Bootstrap.Security; using Longbow.Cache; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -35,20 +34,12 @@ namespace Bootstrap.DataAccess public static bool DeleteDict(IEnumerable value) { var ret = false; - try + var ids = string.Join(",", value); + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts where ID in ({0})", ids); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - var ids = string.Join(",", value); - string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts where ID in ({0})", ids); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - CacheCleanUtility.ClearCache(dictIds: ids); - ret = true; - } - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count(); + CacheCleanUtility.ClearCache(dictIds: ids); } return ret; } @@ -67,24 +58,16 @@ namespace Bootstrap.DataAccess string sql = p.Id == 0 ? "Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" : "Update Dicts set Category = @Category, Name = @Name, Code = @Code, Define = @Define where ID = @ID"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - ret = true; - CacheCleanUtility.ClearCache(dictIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(dictIds: p.Id == 0 ? string.Empty : p.Id.ToString()); return ret; } /// @@ -98,22 +81,14 @@ namespace Bootstrap.DataAccess { var ret = false; string sql = "Update Dicts set Code = @Code where Category = @Category and Name = @Name"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", dict.Name)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", dict.Code)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", dict.Category)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(dictIds: string.Empty); - ret = true; - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", dict.Name)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", dict.Code)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", dict.Category)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(dictIds: string.Empty); return ret; } /// @@ -127,17 +102,13 @@ namespace Bootstrap.DataAccess var ret = new List(); string sql = "select distinct Category from Dicts"; DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) - { - ret.Add(new BootstrapDict() { Category = (string)reader[0] }); - } + ret.Add(new BootstrapDict() { Category = (string)reader[0] }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return ret; }); } diff --git a/Bootstrap.DataAccess/GroupHelper.cs b/Bootstrap.DataAccess/GroupHelper.cs index 12fe449b..9cba9dae 100644 --- a/Bootstrap.DataAccess/GroupHelper.cs +++ b/Bootstrap.DataAccess/GroupHelper.cs @@ -1,6 +1,5 @@ using Longbow.Cache; using Longbow.Data; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -31,22 +30,18 @@ namespace Bootstrap.DataAccess string sql = "select * from Groups"; List groups = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + groups.Add(new Group() { - groups.Add(new Group() - { - Id = (int)reader[0], - GroupName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2] - }); - } + Id = (int)reader[0], + GroupName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return groups; }); return id == 0 ? ret : ret.Where(t => id == t.Id); @@ -58,21 +53,13 @@ namespace Bootstrap.DataAccess public static bool DeleteGroup(IEnumerable value) { bool ret = false; - try + var ids = string.Join(",", value); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups")) { - var ids = string.Join(",", value); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups")) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(groupIds: ids); - ret = true; - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; } + CacheCleanUtility.ClearCache(groupIds: ids); return ret; } /// @@ -88,22 +75,14 @@ namespace Bootstrap.DataAccess string sql = p.Id == 0 ? "Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" : "Update Groups set GroupName = @GroupName, Description = @Description where ID = @ID"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupName", p.GroupName)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description))); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(groupIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - ret = true; - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupName", p.GroupName)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description))); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(groupIds: p.Id == 0 ? string.Empty : p.Id.ToString()); return ret; } /// @@ -120,23 +99,19 @@ namespace Bootstrap.DataAccess List groups = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId)); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + groups.Add(new Group() { - groups.Add(new Group() - { - Id = (int)reader[0], - GroupName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + GroupName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return groups; }, RetrieveGroupsByUserIdDataKey); return ret; @@ -182,8 +157,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; @@ -202,23 +177,19 @@ namespace Bootstrap.DataAccess string sql = "select g.ID,g.GroupName,g.[Description],case rg.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join RoleGroup rg on g.ID=rg.GroupID and RoleID=@RoleID"; DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + groups.Add(new Group() { - groups.Add(new Group() - { - Id = (int)reader[0], - GroupName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + GroupName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return groups; }, RetrieveGroupsByRoleIdDataKey); } @@ -261,8 +232,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; diff --git a/Bootstrap.DataAccess/LogHelper.cs b/Bootstrap.DataAccess/LogHelper.cs index c4c572ec..c1c0261c 100644 --- a/Bootstrap.DataAccess/LogHelper.cs +++ b/Bootstrap.DataAccess/LogHelper.cs @@ -1,5 +1,4 @@ using Longbow.Cache; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -24,26 +23,22 @@ namespace Bootstrap.DataAccess string sql = "select top 1000 * from Logs"; List logs = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + logs.Add(new Log() { - logs.Add(new Log() - { - Id = (int)reader[0], - CRUD = (string)reader[1], - UserName = (string)reader[2], - LogTime = (DateTime)reader[3], - ClientIp = (string)reader[4], - ClientAgent = (string)reader[5], - RequestUrl = (string)reader[6] - }); - } + Id = (int)reader[0], + CRUD = (string)reader[1], + UserName = (string)reader[2], + LogTime = (DateTime)reader[3], + ClientIp = (string)reader[4], + ClientAgent = (string)reader[5], + RequestUrl = (string)reader[6] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return logs; }); return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase)); @@ -56,20 +51,12 @@ namespace Bootstrap.DataAccess public static bool DeleteLog(IEnumerable value) { bool ret = false; - try + var ids = string.Join(",", value); + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - var ids = string.Join(",", value); - string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - CacheCleanUtility.ClearCache(logIds: ids); - ret = true; - } - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count(); + CacheCleanUtility.ClearCache(logIds: ids); } return ret; } @@ -83,24 +70,16 @@ namespace Bootstrap.DataAccess if (p == null) throw new ArgumentNullException("p"); bool ret = false; string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, GetDate(), @ClientIp, @ClientAgent, @RequestUrl)"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@CRUD", p.CRUD)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(logIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - ret = true; - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@CRUD", p.CRUD)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(logIds: p.Id == 0 ? string.Empty : p.Id.ToString()); return ret; } } diff --git a/Bootstrap.DataAccess/MenuHelper.cs b/Bootstrap.DataAccess/MenuHelper.cs index 48b83043..84b9b63a 100644 --- a/Bootstrap.DataAccess/MenuHelper.cs +++ b/Bootstrap.DataAccess/MenuHelper.cs @@ -1,7 +1,6 @@ using Bootstrap.Security; using Longbow.Cache; using Longbow.Data; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -27,21 +26,13 @@ namespace Bootstrap.DataAccess public static bool DeleteMenu(IEnumerable value) { bool ret = false; - try + var ids = string.Join(",", value); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus")) { - var ids = string.Join(",", value); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus")) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(menuIds: ids); - ret = true; - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count(); } + CacheCleanUtility.ClearCache(menuIds: ids); return ret; } /// @@ -59,29 +50,21 @@ namespace Bootstrap.DataAccess string sql = p.Id == 0 ? "Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category, Target, IsResource, [Application]) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category, @Target, @IsResource, @ApplicationCode)" : "Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category, Target = @Target, IsResource = @IsResource, Application = @ApplicationCode where ID = @ID"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccessFactory.ToDBValue(p.Icon))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccessFactory.ToDBValue(p.Url))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@IsResource", p.IsResource)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - ret = true; - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccessFactory.ToDBValue(p.Icon))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccessFactory.ToDBValue(p.Url))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@IsResource", p.IsResource)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? string.Empty : p.Id.ToString()); return ret; } @@ -96,25 +79,21 @@ namespace Bootstrap.DataAccess return CacheManager.GetOrAdd(key, k => { var menus = new List(); - try + string sql = "select NavigationID from NavigationRole where RoleID = @RoleID"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - string sql = "select NavigationID from NavigationRole where RoleID = @RoleID"; - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + menus.Add(new BootstrapMenu() { - menus.Add(new BootstrapMenu() - { - Id = (int)reader[0] - }); - } + Id = (int)reader[0] + }); } } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return menus; }, RetrieveMenusByRoleIdDataKey); } @@ -156,8 +135,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; diff --git a/Bootstrap.DataAccess/MessageHelper.cs b/Bootstrap.DataAccess/MessageHelper.cs index 04cd0f85..fdfcd483 100644 --- a/Bootstrap.DataAccess/MessageHelper.cs +++ b/Bootstrap.DataAccess/MessageHelper.cs @@ -1,6 +1,5 @@ using Longbow; using Longbow.Cache; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -27,33 +26,29 @@ namespace Bootstrap.DataAccess string sql = "select m.*, d.Name, isnull(i.Code + u.Icon, '~/images/uploader/default.jpg'), u.DisplayName from [Messages] m left join Dicts d on m.Label = d.Code and d.Category = N'消息标签' and d.Define = 0 left join Dicts i on i.Category = N'头像地址' and i.Name = N'头像路径' and i.Define = 0 inner join Users u on m.[From] = u.UserName where [To] = @UserName or [From] = @UserName order by m.SendTime desc"; List messages = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", userName)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", userName)); - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + messages.Add(new Message() { - messages.Add(new Message() - { - Id = (int)reader[0], - Title = (string)reader[1], - Content = (string)reader[2], - From = (string)reader[3], - To = (string)reader[4], - SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), - Status = (string)reader[6], - Mark = (int)reader[7], - IsDelete = (int)reader[8], - Label = (string)reader[9], - LabelName = LgbConvert.ReadValue(reader[10], string.Empty), - FromIcon = (string)reader[11], - FromDisplayName = (string)reader[12] - }); - } + Id = (int)reader[0], + Title = (string)reader[1], + Content = (string)reader[2], + From = (string)reader[3], + To = (string)reader[4], + SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), + Status = (string)reader[6], + Mark = (int)reader[7], + IsDelete = (int)reader[8], + Label = (string)reader[9], + LabelName = LgbConvert.ReadValue(reader[10], string.Empty), + FromIcon = (string)reader[11], + FromDisplayName = (string)reader[12] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return messages; }); diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index ebd5aed7..9aaf431b 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -1,6 +1,5 @@ using Longbow.Cache; using Longbow.Data; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -31,22 +30,18 @@ namespace Bootstrap.DataAccess string sql = "select * from Roles"; List roles = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + roles.Add(new Role() { - roles.Add(new Role() - { - Id = (int)reader[0], - RoleName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2] - }); - } + Id = (int)reader[0], + RoleName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return roles; }); return id == 0 ? ret : ret.Where(t => id == t.Id); @@ -93,8 +88,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; @@ -110,25 +105,21 @@ namespace Bootstrap.DataAccess { List roles = new List(); string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @UserID"; - try + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId)); - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + roles.Add(new Role() { - roles.Add(new Role() - { - Id = (int)reader[0], - RoleName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + RoleName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return roles; }, RetrieveRolesByUserIdDataKey); } @@ -139,21 +130,13 @@ namespace Bootstrap.DataAccess public static bool DeleteRole(IEnumerable value) { bool ret = false; - try + var ids = string.Join(",", value); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles")) { - var ids = string.Join(",", value); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles")) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(roleIds: ids); - ret = true; - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; } + CacheCleanUtility.ClearCache(roleIds: ids); return ret; } /// @@ -169,22 +152,14 @@ namespace Bootstrap.DataAccess string sql = p.Id == 0 ? "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" : "Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID"; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description))); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(roleIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - ret = true; - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description))); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } + CacheCleanUtility.ClearCache(roleIds: p.Id == 0 ? string.Empty : p.Id.ToString()); return ret; } /// @@ -201,23 +176,19 @@ namespace Bootstrap.DataAccess List roles = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId)); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + roles.Add(new Role() { - roles.Add(new Role() - { - Id = (int)reader[0], - RoleName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + RoleName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return roles; }, RetrieveRolesByMenuIdDataKey); return ret; @@ -257,8 +228,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; @@ -276,24 +247,20 @@ namespace Bootstrap.DataAccess List roles = new List(); string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID"; DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupId)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupId)); - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + roles.Add(new Role() { - roles.Add(new Role() - { - Id = (int)reader[0], - RoleName = (string)reader[1], - Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + RoleName = (string)reader[1], + Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return roles; }, RetrieveRolesByGroupIdDataKey); } @@ -339,8 +306,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; diff --git a/Bootstrap.DataAccess/TaskHelper.cs b/Bootstrap.DataAccess/TaskHelper.cs index 5a00cb7b..b840451f 100644 --- a/Bootstrap.DataAccess/TaskHelper.cs +++ b/Bootstrap.DataAccess/TaskHelper.cs @@ -1,5 +1,4 @@ using Longbow.Cache; -using Longbow.Logging; using System; using System.Collections.Generic; using System.Data; @@ -21,27 +20,23 @@ namespace Bootstrap.DataAccess string sql = "select top 1000 t.*, u.DisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc"; List tasks = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + tasks.Add(new Task() { - tasks.Add(new Task() - { - Id = (int)reader[0], - TaskName = (string)reader[1], - AssignName = (string)reader[2], - UserName = (string)reader[3], - TaskTime = (int)reader[4], - TaskProgress = (double)reader[5], - AssignTime = (DateTime)reader[6], - AssignDisplayName = (string)reader[7] - }); - } + Id = (int)reader[0], + TaskName = (string)reader[1], + AssignName = (string)reader[2], + UserName = (string)reader[3], + TaskTime = (int)reader[4], + TaskProgress = (double)reader[5], + AssignTime = (DateTime)reader[6], + AssignDisplayName = (string)reader[7] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return tasks; }); }