示例#1
0
        /// <summary>
        /// Writes the first <see cref="DataTable"/> in <paramref name="dataSet"/> as CSV data.
        /// </summary>
        /// <remarks>
        /// This method writes all the data in the first table of <paramref name="dataSet"/> to this <c>CsvWriter</c>, optionally writing a header
        /// record based on the columns in the table.
        /// </remarks>
        /// <param name="provider">
        /// The format provider to use for any values in the data set that implement <see cref="IConvertible"/>.
        /// </param>
        /// <param name="dataSet">
        /// The <c>DataSet</c> whose first table is to be written as CSV data.
        /// </param>
        /// <param name="writeHeaderRecord">
        /// If <see langword="true"/>, a CSV header will be written based on the column names for the table.
        /// </param>
        public void WriteAll(IFormatProvider provider, DataSet dataSet, bool writeHeaderRecord)
        {
            EnsureNotDisposed();
            dataSet.AssertNotNull("dataSet");
            ExceptionHelper.ResolveAndThrowIf(dataSet.Tables.Count == 0, "WriteAll.dataSet-no-table");

            WriteAll(provider, dataSet.Tables[0], writeHeaderRecord);
        }
        /// <summary>
        /// Creates a table in <paramref name="this"/> and populates it with data read from <paramref name="csvReader"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
        /// </para>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataSet"/>.
        /// </param>
        /// <param name="csvReader">
        /// The <see cref="CsvReader"/>.
        /// </param>
        /// <param name="tableName">
        /// The name of the table to create and add to <paramref name="this"/>
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read and add to the <see cref="DataTable"/>.
        /// </param>
        /// <returns>
        /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
        /// </returns>
        public static int Fill(this DataSet @this, CsvReader csvReader, string tableName, int?maximumRecords)
        {
            @this.AssertNotNull("@this");
            tableName.AssertNotNull("tableName");

            var table = @this.Tables.Add(tableName);

            return(table.Fill(csvReader, maximumRecords));
        }
示例#3
0
        /// <summary>
        /// Creates a table in <paramref name="this"/> and populates it with data read asynchronously from <paramref name="csvReader"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
        /// </para>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataSet"/>.
        /// </param>
        /// <param name="csvReader">
        /// The <see cref="CsvReader"/>.
        /// </param>
        /// <param name="tableName">
        /// The name of the table to create and add to <paramref name="this"/>
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read and add to the <see cref="DataTable"/>.
        /// </param>
        /// <returns>
        /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
        /// </returns>
        public async static Task <int> FillAsync(this DataSet @this, CsvReader csvReader, string tableName, int?maximumRecords)
        {
            @this.AssertNotNull("@this");
            tableName.AssertNotNull("tableName");

            var table = @this.Tables.Add(tableName);

            return(await table.FillAsync(csvReader, maximumRecords).ConfigureAwait(false));
        }
示例#4
0
        /// <summary>
        /// Fills the specified <see cref="DataSet"/> with CSV data.
        /// </summary>
        /// <remarks>
        /// The header record for the <c>CsvReader</c> must be set prior to invoking this method. The data read must conform to the header record. That
        /// is, if a record is found with more columns than specified by the header record, an exception will be thrown.
        /// </remarks>
        /// <param name="dataSet">
        /// The <c>DataSet</c> to be filled.
        /// </param>
        /// <param name="tableName">
        /// The name for the table created in the <c>DataSet</c> that holds the CSV data.
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read. Only relevant if <paramref name="useMaximum"/> is <see langword="true"/>.
        /// </param>
        /// <param name="useMaximum">
        /// If <see langword="true"/>, <paramref name="maximumRecords"/> takes affect. Otherwise, no limit is imposed on the number of records read.
        /// </param>
        /// <returns>
        /// The number of records read and stored in <paramref name="dataSet"/>.
        /// </returns>
        private int Fill(DataSet dataSet, string tableName, int maximumRecords, bool useMaximum)
        {
            EnsureNotDisposed();
            dataSet.AssertNotNull("dataSet");
            tableName.AssertNotNull("tableName");
            ExceptionHelper.ResolveAndThrowIf(useMaximum && (maximumRecords < 0), "Fill.maximumRecords-less-than-zero", "maximumRecords");
            ExceptionHelper.ResolveAndThrowIf(m_headerRecord == null, "Fill.no-header-record-set");

            DataTable table = dataSet.Tables.Add(tableName);

            //set up the table columns based on the header record
            if (m_headerRecord != null)
            {
                foreach (string column in m_headerRecord.Values)
                {
                    table.Columns.Add(column);
                }
            }

            int num = 0;

            while (!useMaximum || (num < maximumRecords))
            {
                string[] record = ReadDataRecordAsStrings();

                if (record != null)
                {
                    if (m_headerRecord != null)
                    {
                        ExceptionHelper.ResolveAndThrowIf(m_headerRecord != null && record.Length > m_headerRecord.Values.Count, "Fill.too-many-columns-in-record", record.Length, m_headerRecord.Values.Count);
                    }

                    string[] recordAsStrings = new string[record.Length];
                    record.CopyTo(recordAsStrings, 0);
// ReSharper disable CoVariantArrayConversion
                    table.Rows.Add(recordAsStrings);
// ReSharper restore CoVariantArrayConversion
                    ++num;
                }
                else
                {
                    break;
                }
            }

            return(num);
        }