当前位置: 首页>>代码示例>>C#>>正文


C# DataRecord.Add方法代码示例

本文整理汇总了C#中DataRecord.Add方法的典型用法代码示例。如果您正苦于以下问题:C# DataRecord.Add方法的具体用法?C# DataRecord.Add怎么用?C# DataRecord.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataRecord的用法示例。


在下文中一共展示了DataRecord.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: issue12693_repro

        public void issue12693_repro()
        {
            var recordCount = 50;
            var random = new Random();
            var memoryStream = new MemoryStream();
            var csvWriter = new CsvWriter(memoryStream);

            // create some dummy data to work with
            for (var record = 0; record < recordCount; ++record)
            {
                var dataRecord = new DataRecord(null);

                for (var value = 0; value < random.Next(100, 200); ++value)
                {
                    dataRecord.Add("value" + random.Next(0, 100000));
                }

                csvWriter.WriteRecord(dataRecord);
            }

            memoryStream.Position = 0;

            // create a fake server that will feed us our dummy data in dribs and drabs
            using (var fakeServer = new FakeServer(memoryStream))
            using (var connectionToServer = new TcpClient("localhost", FakeServer.Port))
            using (var csvReader = new CsvReader(connectionToServer.GetStream()))
            {
                while (csvReader.HasMoreRecords)
                {
                    csvReader.ReadDataRecord();
                }

                // when the bug manifests itself, the parser may stop parsing prematurely so the record counts won't match
                // see below for an explanation of how the bug is triggered
                Assert.Equal(recordCount, csvReader.RecordNumber);
            }
        }
开发者ID:modulexcite,项目名称:KBCsv,代码行数:37,代码来源:Issue12693.cs

示例2: WriteCsvAsync

        /// <summary>
        /// Asynchronously writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataTable"/>.
        /// </param>
        /// <param name="csvWriter">
        /// The <see cref="CsvWriter"/>.
        /// </param>
        /// <param name="writeHeaderRecord">
        /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>.
        /// </param>
        /// <param name="maximumRows">
        /// The maximum number of rows from <paramref name="this"/> that should be written to <paramref name="csvWriter"/>.
        /// </param>
        /// <param name="objectToStringConverter">
        /// Provides a means of converting values in the <see cref="DataRow"/>s to <see cref="String"/>s.
        /// </param>
        /// <returns>
        /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>.
        /// </returns>
        public async static Task<int> WriteCsvAsync(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord, int? maximumRows, Func<object, string> objectToStringConverter)
        {
            @this.AssertNotNull("@this");
            csvWriter.AssertNotNull("csvWriter");
            objectToStringConverter.AssertNotNull("objectToStringConverter");

            var num = 0;

            if (writeHeaderRecord)
            {
                var columnNames = new string[@this.Columns.Count];

                for (var i = 0; i < columnNames.Length; ++i)
                {
                    columnNames[i] = @this.Columns[i].ColumnName;
                }

                await csvWriter.WriteRecordAsync(columnNames).ConfigureAwait(false);
            }

            var maximum = maximumRows.GetValueOrDefault(int.MaxValue);
            var buffer = new DataRecord[16];
            var bufferOffset = 0;

            foreach (DataRow row in @this.Rows)
            {
                var record = new DataRecord();

                for (var i = 0; i < row.ItemArray.Length; ++i)
                {
                    record.Add(objectToStringConverter(row.ItemArray[i]));
                }

                buffer[bufferOffset++] = record;

                if (bufferOffset == buffer.Length)
                {
                    // buffer full
                    await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                    bufferOffset = 0;
                }

                if (++num == maximum)
                {
                    break;
                }
            }

            // write any outstanding data in buffer
            await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false);

            return num;
        }
开发者ID:modulexcite,项目名称:KBCsv,代码行数:76,代码来源:DataExtensions_Async.cs


注:本文中的DataRecord.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。