Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper: Not able to parse string from dbf(Error parsing column)

Tags:

c#

dapper

dbf

I want to use dapper to query over dbf files. In my file example.dbf I have two columns:

  1. Value - type NUMERIC
  2. Name - type CHARACTER

I write class ExampleDbf

class ExampleDbf
{
    public int Value { get; set; }
    public string Name { get; set; }
}

Now I want to write two simple query

var listOne = connection.Query<ExampleDbf>("SELECT value FROM Example");
var listTwo = connection.Query<ExampleDbf>("SELECT name, value FROM Example");

ListOne are OK but when I execute listTwo I Have following System.Data.DataException:

Additional information: Error parsing column 0 (name=System.Byte[] - Object)

When I use standard DataReader I must write something like that

example.name = System.Text.Encoding.ASCII.GetString((byte[])reader["name"]).Trim();

Of course I may write something like this:

class ExampleDbf2
{
    public int Value { get; set; }
    public byte[] Name { get; set; }
    public string StringName
    {
        get
        {
            return System.Text.Encoding.ASCII.GetString((byte[])Name ).Trim();
        }
    }
}

So now it works

var listTwo = connection.Query<ExampleDbf2>("SELECT name, value FROM Example");

But this solution is very ugly, maybe someone have better solution.

like image 441
wopi Avatar asked Apr 22 '15 20:04

wopi


1 Answers

You could always return a dynamic, then map it to your object and perform the transformation operation during object initialization.

var listTwo = connection.Query<dynamic>("SELECT name, value FROM Example")
    .Select(x => new ExampleDbf 
        { 
            Value = x.value,  
            Name = System.Text.Encoding.ASCII.GetString((byte[])x.name).Trim()
        }).ToList();
like image 115
David L Avatar answered Sep 30 '22 13:09

David L