Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

.GetValue() cares about column data type #9

Open
LaurenceTurner opened this issue Aug 18, 2020 · 1 comment
Open

.GetValue() cares about column data type #9

LaurenceTurner opened this issue Aug 18, 2020 · 1 comment
Assignees

Comments

@LaurenceTurner
Copy link

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.

@eXavera eXavera self-assigned this Aug 18, 2020
@eXavera
Copy link
Owner

eXavera commented Aug 18, 2020

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 🙂

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants