/// <summary> /// 此函数只能在从库数据库连接中进行 /// </summary> /// <param name="action"></param> public static object ExecuteScalarSlave(CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters) { object result = null; void Transfer(Exception ex) { if (InstanceSlave != null && InstanceSlave.Pool.ConnectionList.Count > 0) { result = InstanceSlave.ExecuteScalar(commandType, commandText, onExecuted, commandParameters); } else { result = InstanceMaster.ExecuteScalar(commandType, commandText, onExecuted, commandParameters); } } try { Transfer(null); } catch (System.TimeoutException te) { WriteLog(te); Transfer(te); } catch (System.Net.Sockets.SocketException ex) { WriteLog(ex); Transfer(ex); } return(result); }
/// <summary> /// 此函数只能在从库数据库连接中进行 /// </summary> /// <param name="action"></param> public static void ExecuteDataReaderSlave(Action <DbDataReader> action, CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters) { void Transfer(Exception ex) { if (InstanceSlave != null && InstanceSlave.Pool.ConnectionList.Count > 0) { InstanceSlave.ExecuteDataReader(action, commandType, commandText, onExecuted, commandParameters); } else { InstanceMaster.ExecuteDataReader(action, commandType, commandText, onExecuted, commandParameters); } } try { Transfer(null); } catch (System.TimeoutException te) { WriteLog(te); Transfer(te); } catch (System.Net.Sockets.SocketException ex) { WriteLog(ex); Transfer(ex); } }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static void Transaction(Action action) { try { InstanceMaster.BeginTransaction(); action?.Invoke(); InstanceMaster.CommitTransaction(); } catch (Exception e) { InstanceMaster.RollBackTransaction(); throw e; } }
/// <summary> /// 封装多个查询结果集,以管道的形式 /// </summary> /// <param name="master">是否在主库执行查询</param> /// <param name="contexts">查询上下文对象</param> /// <returns></returns> public static List <List <dynamic> > ExecutePipeLine(bool master, params IQueryPipe[] contexts) { CheckNotNull.NotEmpty(contexts, nameof(contexts)); StringBuilder sb = new StringBuilder(); List <DbParameter> parameters = new List <DbParameter>(); foreach (var ctx in contexts) { sb.AppendLine(ctx.CommandText); sb.Append(";"); parameters.AddRange(ctx.ParamList); } var cmdText = sb.ToString(); int pipeLine = contexts.Length; List <List <dynamic> > result = new List <List <dynamic> >(); if (master) { CheckNotNull.NotNull(InstanceMaster, nameof(InstanceMaster)); InstanceMaster.ExecuteDataReaderPipe(dr => { ExcutePipeResult(contexts, dr, pipeLine, result); }, CommandType.Text, cmdText, parameters.ToArray()); } else { CheckNotNull.NotNull(InstanceSlave, nameof(InstanceSlave)); InstanceSlave.ExecuteDataReaderPipe(dr => { ExcutePipeResult(contexts, dr, pipeLine, result); }, CommandType.Text, cmdText, parameters.ToArray()); } return(result); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static void ExecuteDataReader(Action <DbDataReader> action, CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters) { InstanceMaster.ExecuteDataReader(action, commandType, commandText, onExecuted, commandParameters); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static int ExecuteNonQuery(CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters) { return(InstanceMaster.ExecuteNonQuery(commandType, commandText, onExecuted, commandParameters)); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static object ExecuteScalar(CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters) { return(InstanceMaster.ExecuteScalar(commandType, commandText, onExecuted, commandParameters)); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static object ExecuteScalar(CommandType commandType, string commandText, params DbParameter[] commandParameters) { return(InstanceMaster.ExecuteScalar(commandType, commandText, null, commandParameters)); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static void ExecuteDataReader(Action <NpgsqlDataReader> action, CommandType commandType, string commandText, params NpgsqlParameter[] commandParameters) { InstanceMaster.ExecuteDataReader(action, commandType, commandText, commandParameters); }
/// <summary> /// 此函数只能在读写数据库连接中进行 /// </summary> /// <param name="action"></param> public static int ExecuteNonQuery(CommandType commandType, string commandText, params NpgsqlParameter[] commandParameters) { return(InstanceMaster.ExecuteNonQuery(commandType, commandText, commandParameters)); }