Пример #1
0
        /// <summary>
        /// 执行查询命令并返回执行上下文
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <param name="tracing">用于追踪查询过程的追踪器</param>
        /// <returns>查询执行上下文</returns>
        protected virtual IDbExecuteContext Execute(MySqlCommand command, IDbTracing tracing = null)
        {
            try
            {
                TryExecuteTracing(tracing, t => t.OnExecuting(command));


                var connection = new MySqlConnection(ConnectionString);
                connection.Open();
                command.Connection = connection;

                if (Configuration.QueryExecutingTimeout.HasValue)
                {
                    command.CommandTimeout = (int)Configuration.QueryExecutingTimeout.Value.TotalSeconds;
                }


                var reader  = command.ExecuteReader();
                var context = new SqlDbExecuteContext(connection, reader, tracing);

                TryExecuteTracing(tracing, t => t.OnLoadingData(context));

                return(context);
            }
            catch (DbException exception)
            {
                TryExecuteTracing(tracing, t => t.OnException(exception));
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// 异步执行查询命令并返回执行上下文
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <param name="token">取消指示</param>
        /// <param name="tracing">用于追踪查询过程的追踪器</param>
        /// <returns>查询执行上下文</returns>
        protected virtual async Task <IAsyncDbExecuteContext> ExecuteAsync(MySqlCommand command, CancellationToken token, IDbTracing tracing = null)
        {
            try
            {
                TryExecuteTracing(tracing, t => t.OnExecuting(command));

                var connection = new MySqlConnection(ConnectionString);
                await connection.OpenAsync(token);

                command.Connection = connection;

                if (Configuration.QueryExecutingTimeout.HasValue)
                {
                    command.CommandTimeout = (int)Configuration.QueryExecutingTimeout.Value.TotalSeconds;
                }


                var reader = await command.ExecuteReaderAsync(token);

                var context = new SqlDbExecuteContext(connection, reader, tracing);

                TryExecuteTracing(tracing, t => t.OnLoadingData(context));

                return(context);
            }
            catch (DbException exception)
            {
                TryExecuteTracing(tracing, t => t.OnException(exception));
                throw;
            }
        }