重构代码:移除Try/Catch模块,数据库操作使用影响行数判断是否成功

This commit is contained in:
Argo-MacBookPro 2018-09-08 19:57:42 +08:00
parent e556556e7c
commit 52427c77b2
7 changed files with 187 additions and 330 deletions

View File

@ -1,6 +1,5 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache; using Longbow.Cache;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -35,20 +34,12 @@ namespace Bootstrap.DataAccess
public static bool DeleteDict(IEnumerable<int> value) public static bool DeleteDict(IEnumerable<int> value)
{ {
var ret = false; 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); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count();
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts where ID in ({0})", ids); CacheCleanUtility.ClearCache(dictIds: 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);
} }
return ret; return ret;
} }
@ -67,24 +58,16 @@ namespace Bootstrap.DataAccess
string sql = p.Id == 0 ? string sql = p.Id == 0 ?
"Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" : "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"; "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("@ID", p.Id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(dictIds: p.Id == 0 ? string.Empty : p.Id.ToString());
return ret; return ret;
} }
/// <summary> /// <summary>
@ -98,22 +81,14 @@ namespace Bootstrap.DataAccess
{ {
var ret = false; var ret = false;
string sql = "Update Dicts set Code = @Code where Category = @Category and Name = @Name"; 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("@Name", dict.Name)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", dict.Category));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", dict.Code)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(dictIds: string.Empty);
return ret; return ret;
} }
/// <summary> /// <summary>
@ -127,17 +102,13 @@ namespace Bootstrap.DataAccess
var ret = new List<BootstrapDict>(); var ret = new List<BootstrapDict>();
string sql = "select distinct Category from Dicts"; string sql = "select distinct Category from Dicts";
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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; return ret;
}); });
} }

View File

@ -1,6 +1,5 @@
using Longbow.Cache; using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -31,22 +30,18 @@ namespace Bootstrap.DataAccess
string sql = "select * from Groups"; string sql = "select * from Groups";
List<Group> groups = new List<Group>(); List<Group> groups = new List<Group>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
GroupName = (string)reader[1], });
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return groups; return groups;
}); });
return id == 0 ? ret : ret.Where(t => id == t.Id); return id == 0 ? ret : ret.Where(t => id == t.Id);
@ -58,21 +53,13 @@ namespace Bootstrap.DataAccess
public static bool DeleteGroup(IEnumerable<int> value) public static bool DeleteGroup(IEnumerable<int> value)
{ {
bool ret = false; bool ret = false;
try var ids = string.Join(",", value);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups"))
{ {
var ids = string.Join(",", value); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups")) ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1;
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(groupIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
} }
CacheCleanUtility.ClearCache(groupIds: ids);
return ret; return ret;
} }
/// <summary> /// <summary>
@ -88,22 +75,14 @@ namespace Bootstrap.DataAccess
string sql = p.Id == 0 ? string sql = p.Id == 0 ?
"Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" : "Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" :
"Update Groups set GroupName = @GroupName, Description = @Description where ID = @ID"; "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("@ID", p.Id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupName", p.GroupName)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(groupIds: p.Id == 0 ? string.Empty : p.Id.ToString());
return ret; return ret;
} }
/// <summary> /// <summary>
@ -120,23 +99,19 @@ namespace Bootstrap.DataAccess
List<Group> groups = new List<Group>(); List<Group> groups = new List<Group>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId)); 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],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
GroupName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return groups; return groups;
}, RetrieveGroupsByUserIdDataKey); }, RetrieveGroupsByUserIdDataKey);
return ret; return ret;
@ -182,8 +157,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; 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"; 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); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); 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],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
GroupName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return groups; return groups;
}, RetrieveGroupsByRoleIdDataKey); }, RetrieveGroupsByRoleIdDataKey);
} }
@ -261,8 +232,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; return ret;

View File

@ -1,5 +1,4 @@
using Longbow.Cache; using Longbow.Cache;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -24,26 +23,22 @@ namespace Bootstrap.DataAccess
string sql = "select top 1000 * from Logs"; string sql = "select top 1000 * from Logs";
List<Log> logs = new List<Log>(); List<Log> logs = new List<Log>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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],
Id = (int)reader[0], UserName = (string)reader[2],
CRUD = (string)reader[1], LogTime = (DateTime)reader[3],
UserName = (string)reader[2], ClientIp = (string)reader[4],
LogTime = (DateTime)reader[3], ClientAgent = (string)reader[5],
ClientIp = (string)reader[4], RequestUrl = (string)reader[6]
ClientAgent = (string)reader[5], });
RequestUrl = (string)reader[6]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return logs; return logs;
}); });
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase)); 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<int> value) public static bool DeleteLog(IEnumerable<int> value)
{ {
bool ret = false; 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); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count();
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids); CacheCleanUtility.ClearCache(logIds: 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);
} }
return ret; return ret;
} }
@ -83,24 +70,16 @@ namespace Bootstrap.DataAccess
if (p == null) throw new ArgumentNullException("p"); if (p == null) throw new ArgumentNullException("p");
bool ret = false; bool ret = false;
string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, GetDate(), @ClientIp, @ClientAgent, @RequestUrl)"; 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("@CRUD", p.CRUD)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(logIds: p.Id == 0 ? string.Empty : p.Id.ToString());
return ret; return ret;
} }
} }

View File

@ -1,7 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache; using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -27,21 +26,13 @@ namespace Bootstrap.DataAccess
public static bool DeleteMenu(IEnumerable<int> value) public static bool DeleteMenu(IEnumerable<int> value)
{ {
bool ret = false; bool ret = false;
try var ids = string.Join(",", value);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus"))
{ {
var ids = string.Join(",", value); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus")) ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count();
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(menuIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
} }
CacheCleanUtility.ClearCache(menuIds: ids);
return ret; return ret;
} }
/// <summary> /// <summary>
@ -59,29 +50,21 @@ namespace Bootstrap.DataAccess
string sql = p.Id == 0 ? 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)" : "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"; "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("@ID", p.Id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccessFactory.ToDBValue(p.Icon)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccessFactory.ToDBValue(p.Url)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccessFactory.ToDBValue(p.Icon))); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccessFactory.ToDBValue(p.Url))); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@IsResource", p.IsResource));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@IsResource", p.IsResource)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? string.Empty : p.Id.ToString());
return ret; return ret;
} }
@ -96,25 +79,21 @@ namespace Bootstrap.DataAccess
return CacheManager.GetOrAdd(key, k => return CacheManager.GetOrAdd(key, k =>
{ {
var menus = new List<BootstrapMenu>(); var menus = new List<BootstrapMenu>();
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"; cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId));
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); while (reader.Read())
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
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; return menus;
}, RetrieveMenusByRoleIdDataKey); }, RetrieveMenusByRoleIdDataKey);
} }
@ -156,8 +135,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; return ret;

View File

@ -1,6 +1,5 @@
using Longbow; using Longbow;
using Longbow.Cache; using Longbow.Cache;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; 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"; 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<Message> messages = new List<Message>(); List<Message> messages = new List<Message>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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)); while (reader.Read())
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) messages.Add(new Message()
{ {
messages.Add(new Message() Id = (int)reader[0],
{ Title = (string)reader[1],
Id = (int)reader[0], Content = (string)reader[2],
Title = (string)reader[1], From = (string)reader[3],
Content = (string)reader[2], To = (string)reader[4],
From = (string)reader[3], SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue),
To = (string)reader[4], Status = (string)reader[6],
SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), Mark = (int)reader[7],
Status = (string)reader[6], IsDelete = (int)reader[8],
Mark = (int)reader[7], Label = (string)reader[9],
IsDelete = (int)reader[8], LabelName = LgbConvert.ReadValue(reader[10], string.Empty),
Label = (string)reader[9], FromIcon = (string)reader[11],
LabelName = LgbConvert.ReadValue(reader[10], string.Empty), FromDisplayName = (string)reader[12]
FromIcon = (string)reader[11], });
FromDisplayName = (string)reader[12]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return messages; return messages;
}); });

View File

@ -1,6 +1,5 @@
using Longbow.Cache; using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -31,22 +30,18 @@ namespace Bootstrap.DataAccess
string sql = "select * from Roles"; string sql = "select * from Roles";
List<Role> roles = new List<Role>(); List<Role> roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
RoleName = (string)reader[1], });
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles; return roles;
}); });
return id == 0 ? ret : ret.Where(t => id == t.Id); return id == 0 ? ret : ret.Where(t => id == t.Id);
@ -93,8 +88,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; return ret;
@ -110,25 +105,21 @@ namespace Bootstrap.DataAccess
{ {
List<Role> roles = new List<Role>(); List<Role> roles = new List<Role>();
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"; 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); while (reader.Read())
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId));
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = (int)reader[0],
{ RoleName = (string)reader[1],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles; return roles;
}, RetrieveRolesByUserIdDataKey); }, RetrieveRolesByUserIdDataKey);
} }
@ -139,21 +130,13 @@ namespace Bootstrap.DataAccess
public static bool DeleteRole(IEnumerable<int> value) public static bool DeleteRole(IEnumerable<int> value)
{ {
bool ret = false; bool ret = false;
try var ids = string.Join(",", value);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles"))
{ {
var ids = string.Join(",", value); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteRoles")) ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1;
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(roleIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
} }
CacheCleanUtility.ClearCache(roleIds: ids);
return ret; return ret;
} }
/// <summary> /// <summary>
@ -169,22 +152,14 @@ namespace Bootstrap.DataAccess
string sql = p.Id == 0 ? string sql = p.Id == 0 ?
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" : "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID"; "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("@ID", p.Id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccessFactory.ToDBValue(p.Description)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
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);
} }
CacheCleanUtility.ClearCache(roleIds: p.Id == 0 ? string.Empty : p.Id.ToString());
return ret; return ret;
} }
/// <summary> /// <summary>
@ -201,23 +176,19 @@ namespace Bootstrap.DataAccess
List<Role> roles = new List<Role>(); List<Role> roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId)); 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],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles; return roles;
}, RetrieveRolesByMenuIdDataKey); }, RetrieveRolesByMenuIdDataKey);
return ret; return ret;
@ -257,8 +228,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; return ret;
@ -276,24 +247,20 @@ namespace Bootstrap.DataAccess
List<Role> roles = new List<Role>(); List<Role> roles = new List<Role>();
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"; 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); 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)); while (reader.Read())
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = (int)reader[0],
{ RoleName = (string)reader[1],
Id = (int)reader[0], Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles; return roles;
}, RetrieveRolesByGroupIdDataKey); }, RetrieveRolesByGroupIdDataKey);
} }
@ -339,8 +306,8 @@ namespace Bootstrap.DataAccess
} }
catch (Exception ex) catch (Exception ex)
{ {
ExceptionManager.Publish(ex);
transaction.RollbackTransaction(); transaction.RollbackTransaction();
throw ex;
} }
} }
return ret; return ret;

View File

@ -1,5 +1,4 @@
using Longbow.Cache; using Longbow.Cache;
using Longbow.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; 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"; 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<Task> tasks = new List<Task>(); List<Task> tasks = new List<Task>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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],
Id = (int)reader[0], AssignName = (string)reader[2],
TaskName = (string)reader[1], UserName = (string)reader[3],
AssignName = (string)reader[2], TaskTime = (int)reader[4],
UserName = (string)reader[3], TaskProgress = (double)reader[5],
TaskTime = (int)reader[4], AssignTime = (DateTime)reader[6],
TaskProgress = (double)reader[5], AssignDisplayName = (string)reader[7]
AssignTime = (DateTime)reader[6], });
AssignDisplayName = (string)reader[7]
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return tasks; return tasks;
}); });
} }