Пример #1
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="command"></param>
    /// <param name="dataTable"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static int Fill(this IDbCommand command, DataTable dataTable, CancellationToken cancellationToken)
    {
        Assert.IsNotNull(command);

        var rowCount = 0;

        if (!cancellationToken.IsCancellationRequested)
        {
            var connection = command.Connection;

            using (var connectionStateManager = new ConnectionStateManager(connection))
            {
                connectionStateManager.Open();

                try
                {
                    using (var dataReader = command.ExecuteReader())
                        rowCount = dataReader.Fill(dataTable, cancellationToken);
                }
                catch (Exception exception)
                {
                    throw new DbCommandExecutionException("IDbCommandExtensions.Fill failed.", exception, command);
                }
            }
        }

        return(rowCount);
    }
Пример #2
0
    public static int Fill(this IDbCommand command, DataSet dataSet, CancellationToken cancellationToken)
    {
        Assert.IsNotNull(command);
        Assert.IsNotNull(dataSet);

        var rowCount    = 0;
        var resultIndex = 0;
        var dataTables  = dataSet.Tables;

        if (!cancellationToken.IsCancellationRequested)
        {
            var connection = command.Connection;

            using (var connectionStateManager = new ConnectionStateManager(connection))
            {
                connectionStateManager.Open();

                using (var reader = command.ExecuteReader())
                {
                    while (true)
                    {
                        var fieldCount = reader.FieldCount;

                        if (fieldCount > 0)
                        {
                            DataTable table;

                            if (resultIndex < dataTables.Count)
                            {
                                table = dataTables[resultIndex];
                            }
                            else
                            {
                                table = new DataTable
                                {
                                    Locale = CultureInfo.InvariantCulture
                                };
                                dataSet.Tables.Add(table);
                            }

                            var count = reader.Fill(table, cancellationToken);
                            rowCount += count;
                        }

                        if (!cancellationToken.IsCancellationRequested)
                        {
                            var nextResult = reader.NextResult();

                            if (!nextResult)
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }

                        resultIndex++;
                    }
                }
            }
        }

        return(rowCount);
    }