コード例 #1
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();
        }
コード例 #2
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();
        }
コード例 #3
0
        public void DbNullTest()
        {
            var s = new StringBuilder();

            s.AppendLine(",null");
            using (var reader = new StringReader(s.ToString()))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.HasHeaderRecord = false;
                    csv.Configuration.TypeConverterOptionsCache.GetOptions <string>().NullValues.Add("null");

                    var dataReader = new CsvDataReader(csv);
                    Assert.AreEqual(string.Empty, dataReader.GetValue(0));
                    Assert.AreEqual(DBNull.Value, dataReader.GetValue(1));

                    var values = new object[2];
                    dataReader.GetValues(values);
                    Assert.AreEqual(string.Empty, values[0]);
                    Assert.AreEqual(DBNull.Value, values[1]);
                }
        }
コード例 #4
0
        public void GetValuesTest()
        {
            var s = new StringBuilder();

            s.AppendLine("Boolean,Byte,Bytes,Char,Chars,DateTime,Decimal,Double,Float,Guid,Short,Int,Long,Null");
            s.AppendLine("true,1,0x0102,a,ab,1/1/2019,1.23,4.56,7.89,eca0c8c6-9a2a-4e6c-8599-3561abda13f1,1,2,3,null");
            using (var reader = new StringReader(s.ToString()))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.Delimiter = ",";
                    csv.Configuration.TypeConverterOptionsCache.GetOptions <string>().NullValues.Add("null");
                    var dataReader = new CsvDataReader(csv);
                    dataReader.Read();

                    Assert.AreEqual(true, dataReader.GetBoolean(0));
                    Assert.AreEqual(1, dataReader.GetByte(1));

                    byte[] byteBuffer = new byte[2];
                    dataReader.GetBytes(2, 0, byteBuffer, 0, byteBuffer.Length);
                    Assert.AreEqual(0x1, byteBuffer[0]);
                    Assert.AreEqual(0x2, byteBuffer[1]);

                    Assert.AreEqual('a', dataReader.GetChar(3));

                    char[] charBuffer = new char[2];
                    dataReader.GetChars(4, 0, charBuffer, 0, charBuffer.Length);
                    Assert.AreEqual('a', charBuffer[0]);
                    Assert.AreEqual('b', charBuffer[1]);

                    Assert.IsNull(dataReader.GetData(0));
                    Assert.AreEqual(DateTime.Parse("1/1/2019"), dataReader.GetDateTime(5));
                    Assert.AreEqual(typeof(string).Name, dataReader.GetDataTypeName(0));
                    Assert.AreEqual(1.23m, dataReader.GetDecimal(6));
                    Assert.AreEqual(4.56d, dataReader.GetDouble(7));
                    Assert.AreEqual(typeof(string), dataReader.GetFieldType(0));
                    Assert.AreEqual(7.89f, dataReader.GetFloat(8));
                    Assert.AreEqual(Guid.Parse("eca0c8c6-9a2a-4e6c-8599-3561abda13f1"), dataReader.GetGuid(9));
                    Assert.AreEqual(1, dataReader.GetInt16(10));
                    Assert.AreEqual(2, dataReader.GetInt32(11));
                    Assert.AreEqual(3, dataReader.GetInt64(12));
                    Assert.AreEqual("Boolean", dataReader.GetName(0));
                    Assert.AreEqual(0, dataReader.GetOrdinal("Boolean"));

                    Assert.AreEqual("true", dataReader.GetString(0));
                    Assert.AreEqual("true", dataReader.GetValue(0));

                    var objectBuffer = new object[14];
                    dataReader.GetValues(objectBuffer);
                    Assert.AreEqual("true", objectBuffer[0]);
                    Assert.AreEqual(DBNull.Value, objectBuffer[13]);
                    Assert.IsTrue(dataReader.IsDBNull(13));
                }
        }
コード例 #5
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();
        }
コード例 #6
0
        public override DataTable ToDataTable(DataTableConvertParams context, Stream stream)
        {
            if (context == null)
            {
                context = new DataTableConvertParams();
            }

            StreamReader sr = new StreamReader(stream);
            DataTable    dt = new DataTable();

            if (context.RecordDelim != '\n')
            {
                #region If Record Delimiter is overriden, itterate through all characters and replace them with new line
                //https://stackoverflow.com/questions/1232443/writing-to-then-reading-from-a-memorystream
                MemoryStream ms = new MemoryStream();

                StreamWriter sw = new StreamWriter(ms);
                while (sr.Peek() >= 0)
                {
                    Char c = (Char)sr.Read();
                    if (c == context.RecordDelim)
                    {
                        sw.Write('\n');
                    }
                    else
                    {
                        sw.Write(c);
                    }
                }

                sw.Flush();
                ms.Position = 0;

                sr = new StreamReader(ms, Encoding.UTF8);
                #endregion If Record Delimiter is overriden, itterate through all characters and replace them with new line
            }

            //_conf.BadDataFound = cxt =>
            //{
            //For debugging (put breakpoints here)
            //};

            _conf.Delimiter = context.FieldDelim.ToString();
            if (_conf.Delimiter != ",")
            {
                _conf.IgnoreQuotes = true;
            }
            _conf.HasHeaderRecord   = context.HasHeaders;
            _conf.MissingFieldFound = null;
            CsvReader     csvReader  = new CsvReader(sr, _conf);
            CsvDataReader dataReader = new CsvDataReader(csvReader);

            if (!context.HasHeaders)
            {
                #region If No Headers loop through all records and add columns as columns are found
                while (dataReader.Read())
                {
                    while (dt.Columns.Count < dataReader.FieldCount)
                    {
                        dt.Columns.Add($"Col{dt.Columns.Count}");
                    }

                    DataRow row = dt.NewRow();

                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        row[i] = dataReader.GetValue(i);
                    }

                    dt.Rows.Add(row);
                }

                dataReader.Close();
                sr.Close();
                #endregion If No Headers loop through all records and add columns as columns are found
            }
            else
            {
                #region If there are headers DataTable.Load will suffice
                try
                {
                    dt.Load(dataReader, LoadOption.Upsert);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    dataReader.Close();
                    sr.Close();
                }
                #endregion If there are headers DataTable.Load will suffice
            }

            //CsvHelper seems to make DataTable Columns readonly
            foreach (DataColumn dc in dt.Columns)
            {
                dc.ReadOnly = false;
            }

            return(dt);
        }