Esempio n. 1
0
        internal T ExecuteCommand <T>(SqlCommand command, CommandBehavior behavior)
        {
            Type resultType = typeof(T);

            if (command.Connection == null)
            {
                command.Connection = this._innerConnection;
            }

            var actionResult = ExecuteFailoverPolicy <T>(this._commandRetryPolicy, () =>
            {
                if (typeof(IDataReader).IsAssignableFrom(resultType))
                {
                    var result = command.ExecuteReader(behavior);
                    return((T)(IDataReader)result);
                }
                if (resultType == typeof(XmlReader))
                {
                    throw new NotSupportedException("XmlReader is not supported");
                }

                if (resultType == typeof(ReliableSqlConnection.NonQueryResult))
                {
                    ReliableSqlConnection.NonQueryResult nonQueryResult = new ReliableSqlConnection.NonQueryResult()
                    {
                        RecordsAffected = command.ExecuteNonQuery()
                    };
                    return((T)(object)nonQueryResult);
                }

                var obj = command.ExecuteScalar();
                if (obj != null)
                {
                    if (resultType == typeof(object))
                    {
                        return((T)obj);
                    }
                    return((T)Convert.ChangeType(obj, resultType, (IFormatProvider)CultureInfo.InvariantCulture));
                }
                return(default(T));
            });

            return(actionResult);
        }
Esempio n. 2
0
        internal async Task <T> ExecuteCommandAsync <T>(SqlCommand command, CommandBehavior behavior, CancellationToken cancellationToken)
        {
            Type resultType = typeof(T);

            if (command.Connection == null)
            {
                command.Connection = this._innerConnection;
            }

            var actionResult = await ExecuteFailoverPolicyAsync <T>(this._commandRetryPolicy, new Func <Task <T> >(async() =>
            {
                if (typeof(IDataReader).IsAssignableFrom(resultType))
                {
                    var result = await command.ExecuteReaderAsync(behavior, cancellationToken);
                    return((T)(IDataReader)result);
                }
                if (resultType == typeof(XmlReader))
                {
                    throw new NotSupportedException("XmlReader is not supported");
                }

                if (resultType == typeof(ReliableSqlConnection.NonQueryResult))
                {
                    ReliableSqlConnection.NonQueryResult nonQueryResult = new ReliableSqlConnection.NonQueryResult()
                    {
                        RecordsAffected = await command.ExecuteNonQueryAsync(cancellationToken)
                    };
                    return((T)(object)nonQueryResult);
                }

                var obj = await command.ExecuteScalarAsync(cancellationToken);
                if (obj != null)
                {
                    if (resultType == typeof(object))
                    {
                        return((T)obj);
                    }
                    return((T)Convert.ChangeType(obj, resultType, (IFormatProvider)CultureInfo.InvariantCulture));
                }
                return(default(T));
            }), cancellationToken);

            return(actionResult);
        }