コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
 /// <summary>
 ///  此函数只能在从库数据库连接中进行
 /// </summary>
 /// <param name="action"></param>
 public static int ExecuteNonQuerySlave(CommandType commandType, string commandText, Action <DbCommand> onExecuted = null, params DbParameter[] commandParameters)
 {
     return(InstanceSlave.ExecuteNonQuery(commandType, commandText, onExecuted, commandParameters));
 }