Azavea.Open.DAO.CSV.CsvDaLayer.GetReader C# (CSharp) Méthode

GetReader() protected méthode

Gets a valid StreamReader that can be used to read CSV data. If we were configured with a StreamReader, it may be that one. If we are accessing a file we will open a new reader. You should not close it yourself, instead you should call DoneWithReader when you are done with it.
protected GetReader ( ClassMapping mapping ) : StreamReader
mapping ClassMapping The mapping for the object we intend to read.
Résultat System.IO.StreamReader
        protected internal StreamReader GetReader(ClassMapping mapping)
        {
            switch (_connDesc.Type)
            {
                case CsvConnectionType.Directory:
                case CsvConnectionType.FileName:
                    return new StreamReader(GetFileName(mapping));
                case CsvConnectionType.Reader:
                    // We have a reader specifically set.
                    // Make sure we're back at the beginning.
                    _connDesc.Reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    _connDesc.Reader.DiscardBufferedData();
                    return _connDesc.Reader;
                case CsvConnectionType.Writer:
                    throw new LoggingException("Connection for class " +
                                               mapping + " was configured as read-only: " + _connDesc.Type);
                default:
                    throw new LoggingException("Unable to get reader for class " +
                                               mapping + " for unsupported connection type: " + _connDesc.Type);
            }
        }

Usage Example

Exemple #1
0
        private static DataReaderConfig GetConfig(CsvDaLayer layer, ClassMapping mapping)
        {
            CsvDataReaderConfig retVal = new CsvDataReaderConfig();

            try
            {
                retVal.Reader = layer.GetReader(mapping);
                if (CsvDaLayer.UseNamedColumns(mapping))
                {
                    // If the CSV has row headers, read the header row.
                    IList colNameRow = ReadRawCsvRow(retVal.Reader);
                    for (int x = 0; x < colNameRow.Count; x++)
                    {
                        retVal.IndexesByName[colNameRow[x].ToString()] = x;
                    }
                }
                else
                {
                    // No row headers, so we must be mapped to column numbers.
                    // In that case, just map the column number strings to the ints.
                    foreach (string colStr in mapping.AllDataColsInOrder)
                    {
                        // Remember the mapping column numbers are 1-based but
                        // the internal column numbers are 0-based.
                        retVal.IndexesByName[colStr] = int.Parse(colStr) - 1;
                    }
                }
                return retVal;
            }
            catch (Exception e)
            {
                // Problem setting up, close the reader.
                if (retVal.Reader != null)
                {
                    layer.DoneWithReader(retVal.Reader);
                }
                throw new LoggingException("Unable to begin reading from CSV file.", e);
            }
        }