コード例 #1
0
        public bool Read()
        {
            if (_canceled)
            {
                return(false);
            }

            if (_reader.ReadNextRecord())
            {
                _reader.CopyCurrentRecordTo(_array);
                for (int i = 0; i < _array.Length; i++)
                {
                    if (_dataFormat.IsNullString(_array[i]))
                    {
                        _values[i] = null;
                    }
                    else
                    {
                        _values[i] = _array[i];
                    }
                }
                return(true);
            }
            return(false);
        }
        // Declaration of ReadRecords Method
        /// <summary>
        /// Catches all type of exceptions generated.
        /// </summary>
        /// <param name="passHeader"></param>
        /// <param name="in_delimeter"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public object ReadRecords(string[] passHeader = null, char in_delimeter = ',', string filePath = null)
        {
            try
            {
                if (!filePath.Contains(".csv"))
                {
                    throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INVALID_EXTENSION_OF_FILE, "Invalid Extension of file");
                }
                else if (!filePath.Contains(actualPath))
                {
                    throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.FILE_NOT_FOUND, "Invalid file");
                }

                // streams are used to read/write data from csv files
                //CsvReader is open source C# library to read CSV data from strings/textFiles

                CsvReader csvRecords = new CsvReader(new StreamReader(filePath), true);
                int       fieldCount = csvRecords.FieldCount;
                string[]  headers    = csvRecords.GetFieldHeaders();
                delimeter = csvRecords.Delimiter;

                // string ArrayList
                List <string[]> record = new List <string[]>();
                while (csvRecords.ReadNextRecord())
                {
                    string[] tempRecord = new string[fieldCount];
                    csvRecords.CopyCurrentRecordTo(tempRecord);
                    record.Add(tempRecord);
                    numberOfRecord++;
                }

                if (numberOfRecord == 0)
                {
                    throw new CSVException(CSVException.ExceptionType.FILE_IS_EMPTY, "This file does not contains any data");
                }
                if (!in_delimeter.Equals(delimeter))
                {
                    throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INCORRECT_DELIMETER, "Incorrect Delimeter");
                }
                else if (!IsHeaderSame(passHeader, headers))
                {
                    throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INVALID_HEADER_ERROR, "Invalid Header");
                }
                return(numberOfRecord);
            }
            catch (CensusAnalyserException file_not_found)
            {
                return(file_not_found.Message);
            }
            catch (CSVException emptyFileException)
            {
                return(emptyFileException.Message);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }