private static void Save(SRO obj, string sql, IMyDbParameter[] prams) { string tableName; Type type = obj.GetType(); string[] aliases = obj.Aliases; IDbOperate dbOperate; if (aliases.Length == 0) { tableName = GetTableName(obj, null); dbOperate = DbProxy.Create(null, type); dbOperate.ExecuteNonQuery(string.Format(sql, tableName), prams); } else { foreach (string alias in aliases) { tableName = GetTableName(obj, alias); dbOperate = DbProxy.Create(alias, type); dbOperate.ExecuteNonQuery(string.Format(sql, tableName), prams); } } }
private T LoadObjectDataBaseDirect <T>(string alias, string tableName, string id, bool isNolock) where T : SRO, new() { Type type = typeof(T); IDbOperate dbOperate = DbProxy.Create(alias, type); IDataReader dataReader = null; try { string sql = string.Format(isNolock ? SELECTSINGLEOBJECTWITHNOLOCK : SELECTSINGLEOBJECT, tableName); IMyDbParameter[] prams = { DbParameterProxy.Create("@Id", SqlDbType.NVarChar, 32, id) }; dataReader = dbOperate.GetDataReader(sql, prams); if (!dataReader.Read()) { return(default(T)); } T obj = LoadObject <T>(dataReader); obj.CurrentAlias = alias; obj.CurrentTableName = tableName; return(obj); } catch (Exception ex) { LogProxy.Error(ex, true); throw; } finally { if (dataReader != null) { dataReader.Close(); } } }
private static void Save(SRO obj, string sql, IMyDbParameter[] prams) { string tableName; string executeSql; int executeCnt; Type type = obj.GetType(); string[] aliases = obj.Aliases; IDbOperate dbOperate; if (aliases.Length == 0) { tableName = GetTableName(obj, null); executeSql = string.Format(sql, tableName); dbOperate = DbProxy.Create(null, type); executeCnt = dbOperate.ExecuteNonQuery(executeSql, prams); if (executeCnt != 1) { string jsonPrams = JsonConvert.SerializeObject(prams); LogProxy.Error(string.Format(SAVEFAIL, executeSql, jsonPrams), true); } } else { foreach (string alias in aliases) { tableName = GetTableName(obj, alias); executeSql = string.Format(sql, tableName); dbOperate = DbProxy.Create(alias, type); executeCnt = dbOperate.ExecuteNonQuery(executeSql, prams); if (executeCnt != 1) { string jsonPrams = JsonConvert.SerializeObject(prams); LogProxy.Error(string.Format(SAVEFAIL, executeSql, jsonPrams), true); } } } }
private static QueryOutDTO QueryConstructor(Type type, string tableName, string orderBy, string columnList, QueryInDTO inDto, out DataView dataView) { QueryOutDTO outDto = new QueryOutDTO(); dataView = null; try { Dictionary <string, object> returnValue; IMyDbParameter[] prams = { DbParameterProxy.Create("@TableName", SqlDbType.NVarChar, 50, tableName), DbParameterProxy.Create("@OrderBy", SqlDbType.NVarChar, 500, orderBy), DbParameterProxy.Create("@ColumnList", SqlDbType.NVarChar, 500, columnList), DbParameterProxy.Create("@PageSize", SqlDbType.Int, inDto.PageSize), DbParameterProxy.Create("@PageIndex", SqlDbType.Int, inDto.PageIndex), DbParameterProxy.Create("@Condition", SqlDbType.NVarChar, 4000, inDto.Condition), DbParameterProxy.Create("@PageCount", SqlDbType.Int, ParameterDirection.Output), DbParameterProxy.Create("@RecordCount", SqlDbType.Int, ParameterDirection.Output) }; DataSet dataSet; IDbOperate dbOperate = DbProxy.Create(null, type); dbOperate.RunProcedure(PROPAGING, prams, out dataSet, out returnValue); dataView = dataSet.Tables[0].DefaultView; outDto.IsSucceed = true; outDto.PageCount = (int)returnValue["@PageCount"]; outDto.RecordCount = (int)returnValue["@RecordCount"]; } catch (Exception ex) { LogProxy.Error(ex, false); outDto.ErrorMsg = ex.Message; } return(outDto); }
public static T[] LoadObjects <T>(string alias, string tableName, int top, string condition, IMyDbParameter[] prams, bool isNolock) where T : SRO, new() { Type type = typeof(T); IDbOperate dbOperate = DbProxy.Create(alias, type); IDataReader dataReader = null; try { string sql = string.Format(isNolock ? SELECTOBJECTSWITHNOLOCK : SELECTOBJECTS, (top <= 0 ? "" : "TOP " + top.ToString()), tableName, condition); dataReader = dbOperate.GetDataReader(sql, prams); List <T> list = new List <T>(); while (dataReader.Read()) { list.Add(LoadObject <T>(dataReader)); } return(list.ToArray()); } catch (Exception ex) { LogProxy.Error(ex, true); throw; } finally { if (dataReader != null) { dataReader.Close(); } } }
private static void CreateTable(Type type) { try { string tableName = GetTableName(type); string sqlForTableExists = string.Format(SQLFORTABLEEXISTS, tableName); Queue <string> aliases = GetAlias(type); IDbOperate dbOperate; bool isTableExists; foreach (string alias in aliases) { try { dbOperate = DbProxy.Create(alias, type); isTableExists = IsTableExists(sqlForTableExists, dbOperate); if (isTableExists) { continue; } CreateTable(type, tableName, dbOperate); } catch (Exception ex) { LogProxy.Error(ex, false); } } } catch (Exception ex) { LogProxy.Error(ex, false); } }
public T[] LoadObjects <T>(string alias, string tableName, int top, string condition, IMyDbParameter[] prams, bool isLock, LoadType loadType) where T : SRO, new() { IDataReader dataReader = null; try { string cacheId = null; bool needCache = false; if (loadType == LoadType.CacheFirst && CacheService != null) { needCache = CacheService.IsNeedCached <ArrayCache <T> >(); if (needCache) { string jsonPrams = JsonConvert.SerializeObject(prams); cacheId = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", alias, tableName, top, condition, jsonPrams, isLock); cacheId = Tools.MD5(cacheId); T[] objs = LoadCacheObjects <T>(cacheId); if (objs != null) { return(objs); } } } Type type = typeof(T); string format; if (isLock) { if (string.IsNullOrEmpty(condition)) { format = SELECTOBJECTS; } else { format = SELECTOBJECTSHASCONDITION; } } else { if (string.IsNullOrEmpty(condition)) { format = SELECTOBJECTSWITHNOLOCK; } else { format = SELECTOBJECTSWITHNOLOCKHASCONDITION; } } string sql = string.Format(format, (top <= 0 ? "" : "TOP " + top.ToString()), tableName, condition); IDbOperate dbOperate = DbProxy.Create(alias, type); dataReader = dbOperate.GetDataReader(sql, prams); List <T> list = new List <T>(); while (dataReader.Read()) { list.Add(LoadObject <T>(dataReader)); } T[] t = list.ToArray(); if (needCache) { AddCache <T>(cacheId, t); } return(t); } catch (Exception ex) { LogProxy.Error(ex, true); throw; } finally { if (dataReader != null) { dataReader.Close(); } } }
public static SRO LoadObject(Type type, string alias, string tableName, string id, LoadType loadType, bool isNolock) { if (string.IsNullOrEmpty(id)) { LogProxy.Error(IDISNULL, true); return(null); } if (loadType != LoadType.DataBaseDirect) { SRO obj = LoadCacheObject(type, id) as SRO; if (loadType == LoadType.OnlyCache) { return(obj); } if (obj != null) { return(obj); } } IDbOperate dbOperate = DbProxy.Create(alias, type); IDataReader dataReader = null; try { string sql = string.Format(isNolock ? SELECTSINGLEOBJECTWITHNOLOCK : SELECTSINGLEOBJECT, tableName); IMyDbParameter[] prams = { DbParameterProxy.Create("@Id", SqlDbType.NVarChar, 32, id) }; dataReader = dbOperate.GetDataReader(sql, prams); if (!dataReader.Read()) { return(null); } SRO obj = LoadObject(type, dataReader); if (obj == null) { return(null); } obj.CurrentAlias = alias; obj.CurrentTableName = tableName; return(obj); } catch (Exception ex) { LogProxy.Error(ex, true); throw; } finally { if (dataReader != null) { dataReader.Close(); } } }