NDbfReader icon indicating copy to clipboard operation
NDbfReader copied to clipboard

.GetValue() cares about column data type

Open LaurenceTurner opened this issue 5 years ago • 1 comments

I am trying to read bad data from a DateTime column. Because it cannot parse it to a DateTime it breaks, and I cannot find a suitable alternative method to collect this bad data so I would prefer to use .GetString(myCol) for all columns so I can deal with the parsing and error handling myself.

LaurenceTurner avatar Aug 18 '20 10:08 LaurenceTurner

The simplest solution is to use GetBytes, then convert the bytes to string and parse the string.

Or you can replace the DateTime parsing entirely using custom coumn class:

    class MyDateTimeColumn : DateTimeColumn
    {
        public MyDateTimeColumn(string name, int offset) : base(name, offset)
        {
        }

        protected override DateTime? DoLoad(byte[] buffer, int offset, Encoding encoding)
        {
            string dateTimeString = encoding.GetString(buffer, offset, Size);
            // parse and return the string
        }
    }

    class MyHeaderLoader : HeaderLoader
    {
        protected override Column CreateColumn(byte size, byte type, string name, int columnOffset)
        {
            if (type == NativeColumnType.Date)
            {
                return new MyDateTimeColumn(name, columnOffset);
            }

            return base.CreateColumn(size, type, name, columnOffset);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using var table = Table.Open("C:\\file.dbf", new MyHeaderLoader());
            // use the table
        }
    }

Hope it'll help 🙂

eXavera avatar Aug 18 '20 20:08 eXavera