Dapper: Unable to parse row from dbf (error parsing column)

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

  • Value - Type NUMERIC
  • Name - type CHARACTER

I am writing an ExampleDbf class

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

Now I want to write two simple queries

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

ListOne is fine, but when I execute listTwo I follow a System.Data.DataException:

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

When I use the standard DataReader, I should write something like this

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

Of course, I can 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 has a better solution.

+5
source share
1 answer

You can always return the dynamics, then map it to your object and perform the conversion operation during the initialization of the object.

 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(); 
+6
source

All Articles