Wednesday, December 19, 2012
« Silverlight is “dead” | Main | Testing a WinRT app on a Surface RT »

This is something I use from time to time, and I thought I’d post it here so I can more easily find it next time I need the code.

This is an IDataReader implementation that reads in comma-separated value files. Probably incomplete, but it works well in the scenarios where I’ve needed it.

using System;
using System.Data;
using System.IO;

namespace Csla.Data
{
  public class CsvDataReader : IDataReader, IDisposable
  {
    private StreamReader _file;
    private string[] _headers;
    private string[] _line;

    public CsvDataReader(string filePath)
    {
      _file = File.OpenText(filePath);
      Read();
      _headers = _line;
    }

    public void Close()
    {
      _file.Close();
      _file.Dispose();
      _file = null;
    }

    public int Depth
    {
      get { throw new NotImplementedException(); }
    }

    public DataTable GetSchemaTable()
    {
      throw new NotImplementedException();
    }

    public bool IsClosed
    {
      get { return _file == null; }
    }

    public bool NextResult()
    {
      return false;
    }

    public bool Read()
    {
      var result = !_file.EndOfStream;
      if (result)
        _line = _file.ReadLine().Split(',');
      return result;
    }

    public int RecordsAffected
    {
      get { throw new NotImplementedException(); }
    }

    public void Dispose()
    {
      if (_file != null)
      {
        _file.Dispose();
        _file = null;
      }
    }

    public int FieldCount
    {
      get { return _headers.Length; }
    }

    public bool GetBoolean(int i)
    {
      return Boolean.Parse(_line[i]);
    }

    public byte GetByte(int i)
    {
      return Byte.Parse(_line[i]);
    }

    public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
    {
      throw new NotImplementedException();
    }

    public char GetChar(int i)
    {
      return Char.Parse(_line[i]);
    }

    public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
    {
      throw new NotImplementedException();
    }

    public IDataReader GetData(int i)
    {
      throw new NotImplementedException();
    }

    public string GetDataTypeName(int i)
    {
      throw new NotImplementedException();
    }

    public DateTime GetDateTime(int i)
    {
      return DateTime.Parse(_line[i]);
    }

    public decimal GetDecimal(int i)
    {
      return Decimal.Parse(_line[i]);
    }

    public double GetDouble(int i)
    {
      return Double.Parse(_line[i]);
    }

    public Type GetFieldType(int i)
    {
      throw new NotImplementedException();
    }

    public float GetFloat(int i)
    {
      return float.Parse(_line[i]);
    }

    public Guid GetGuid(int i)
    {
      return Guid.Parse(_line[i]);
    }

    public short GetInt16(int i)
    {
      return Int16.Parse(_line[i]);
    }

    public int GetInt32(int i)
    {
      return Int32.Parse(_line[i]);
    }

    public long GetInt64(int i)
    {
      return Int64.Parse(_line[i]);
    }

    public string GetName(int i)
    {
      return _headers[i];
    }

    public int GetOrdinal(string name)
    {
      int result = -1;
      for (int i = 0; i < _headers.Length; i++)
        if (_headers[i] == name)
        {
          result = i;
          break;
        }
      return result;
    }

    public string GetString(int i)
    {
      return _line[i];
    }

    public object GetValue(int i)
    {
      return _line[i];
    }

    public int GetValues(object[] values)
    {
      throw new NotImplementedException();
    }

    public bool IsDBNull(int i)
    {
      return string.IsNullOrWhiteSpace(_line[i]);
    }

    public object this[string name]
    {
      get { return _line[GetOrdinal(name)]; }
    }

    public object this[int i]
    {
      get { return GetValue(i); }
    }
  }
}
Wednesday, December 19, 2012 10:00:22 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  | 
Comments are closed.