Beispiel #1
0
        public void DisposeReader()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(reader.IsClosed, false);
            reader.Close();
            reader.Dispose();
            reader.Dispose();
        }
Beispiel #2
0
        public void FieldCount()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(3, reader.FieldCount);
            reader.Dispose();
        }
Beispiel #3
0
        public void GetOrdinal()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(0, reader.GetOrdinal("Header1"));
            reader.Dispose();
        }
Beispiel #4
0
        public static IDataTable FromFile(
            string fileName,
            Stream stream,
            long contentLength,
            CsvConfiguration configuration,
            int skip = 0,
            int take = int.MaxValue)
        {
            Guard.NotEmpty(fileName, nameof(fileName));
            Guard.NotNull(stream, nameof(stream));
            Guard.NotNull(configuration, nameof(configuration));

            if (contentLength == 0)
            {
                throw Error.Argument("fileName", "The posted file '{0}' does not contain any data.".FormatInvariant(fileName));
            }

            IDataReader dataReader = null;

            try
            {
                var fileExt = System.IO.Path.GetExtension(fileName).ToLowerInvariant();

                switch (fileExt)
                {
                case ".xlsx":
                    dataReader = new ExcelDataReader(stream, true);     // TODO: let the user specify if excel file has headers
                    break;

                default:
                    dataReader = new CsvDataReader(new StreamReader(stream), configuration);
                    break;
                }

                var table = LightweightDataTable.FromDataReader(dataReader, skip, take);

                if (table.Columns.Count == 0 || table.Rows.Count == 0)
                {
                    throw Error.InvalidOperation("The posted file '{0}' does not contain any columns or data rows.".FormatInvariant(fileName));
                }

                return(table);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Dispose();
                    }
                    dataReader = null;
                }
            }
        }
Beispiel #5
0
        public void CaseInsensitiveColumnNames()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(0, reader.GetOrdinal("header1"));
            Assert.AreEqual(1, reader.GetOrdinal("HEADER2"));
            Assert.AreEqual(2, reader.GetOrdinal("HeaDER3"));
            reader.Dispose();
        }
Beispiel #6
0
        public void ReadAllRows()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            while (reader.Read())
            {
            }
            reader.Close();
            reader.Dispose();
        }
Beispiel #7
0
        public void GetValue()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            reader.Read();
            string v1 = reader.GetValue(0).ToString();

            Assert.AreEqual("Row1A", v1);
            Assert.AreEqual("Row1B", reader.GetValue(1).ToString());
            reader.Dispose();
        }
Beispiel #8
0
        public void MoveFile()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\MoveCsv.txt");

            Assert.AreEqual(reader.IsClosed, false);
            reader.Close();
            Assert.AreEqual(reader.IsClosed, true);
            reader.Dispose();

            File.Move(@"..\..\MoveCsv.txt", @"..\..\MoveCsvTemp.txt");
            File.Move(@"..\..\MoveCsvTemp.txt", @"..\..\MoveCsv.txt");
        }
Beispiel #9
0
        public void EmbeddedComma()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            reader.Read();
            reader.Read();
            Assert.AreEqual(reader.GetValue(0).ToString(), "Quotes");
            string v1       = reader.GetValue(2).ToString();
            string expected = "Q,A";

            Assert.AreEqual(expected, v1);
            reader.Dispose();
        }
Beispiel #10
0
        public void AddStaticValue()
        {
            Dictionary <String, String> staticColumns = new Dictionary <String, String>();

            staticColumns.Add("Column1", "Value");
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt", staticColumns);

            Assert.AreEqual(3, reader.GetOrdinal("Column1"));
            while (reader.Read())
            {
                Assert.AreEqual("Value", reader.GetValue(reader.GetOrdinal("Column1")));
            }
            reader.Close();
            reader.Dispose();
        }
Beispiel #11
0
        public void SimpleOpen()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            reader.Dispose();
        }
        public static IDataTable FromFile(
			string fileName,
			Stream stream,
			long contentLength,
			CsvConfiguration configuration,
			int skip = 0,
			int take = int.MaxValue)
        {
            Guard.ArgumentNotEmpty(() => fileName);
            Guard.ArgumentNotNull(() => stream);
            Guard.ArgumentNotNull(() => configuration);

            if (contentLength == 0)
            {
                throw Error.Argument("fileName", "The posted file '{0}' does not contain any data.".FormatInvariant(fileName));
            }

            IDataReader dataReader = null;

            try
            {
                var fileExt = System.IO.Path.GetExtension(fileName).ToLowerInvariant();

                switch (fileExt)
                {
                    case ".xlsx":
                        dataReader = new ExcelDataReader(stream, true); // TODO: let the user specify if excel file has headers
                        break;
                    default:
                        dataReader = new CsvDataReader(new StreamReader(stream), configuration);
                        break;
                }

                var table = LightweightDataTable.FromDataReader(dataReader, skip, take);

                if (table.Columns.Count == 0 || table.Rows.Count == 0)
                {
                    throw Error.InvalidOperation("The posted file '{0}' does not contain any columns or data rows.".FormatInvariant(fileName));
                }

                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Dispose();
                    }
                    dataReader = null;
                }
            }
        }