0

I am using Filehelpers to read data from a database. I have based my code on the example in: http://filehelpers.sourceforge.net/example_sqlstorage_extract.html

My question is, given my code here, how do I handle binary fields?

public class StudentRecord
{
    public string registration_id;
    public string student_number;
    public string card_number;
    public string firstname;
    public string lastname;

    .... // How do I declare the binary data?
    public BinaryData binarydata;

}

....

protected void FillRecordStudent(object rec, object[] fields)
{
    StudentRecord record = (StudentRecord)rec;

    record.registration_id = (string)fields[0];
    record.student_number = (string)fields[1];
    record.card_number = (string)fields[2];
    record.firstname = (string)fields[3];
    record.lastname = (string)fields[4];

    // how do I do this?????
    record.binarydata = (binary data)fields[5];

    ....
}

Any help would be much appreciated.

Armand Jordaan
  • 348
  • 2
  • 11
  • No idea what is `Filehelpers`, but it should represent a data ( `fields[5]`) somehow. If it's `byte[]`, then your `BinaryData` should have a constructor/method to be initialized from that. – Sinatr Jan 09 '14 at 12:19

1 Answers1

1

For the record, i managed to solve this in the following way (I am not sure if its the most elegant solution but it did work for me):

using FileHelpers;
using FileHelpers.DataLink;
using System.Data.Linq;

public class StudentRecord
{
    public string registration_id;
    public string student_number;
    public string card_number;
    public string firstname;
    public string lastname;

    // a binary field
    [FieldConverter(typeof(BinaryConverter))]
    public Binary binarydata;

}


public class BinaryConverter : ConverterBase
{
    public override object StringToField(string from)
    {
        byte[] temparr = Convert.FromBase64String(from);
        Binary res = new Binary(temparr);

        return res;
    }            
}    

....

protected void FillRecordStudent(object rec, object[] fields)
{
    StudentRecord record = (StudentRecord)rec;

    record.registration_id = (string)fields[0];
    record.student_number = (string)fields[1];
    record.card_number = (string)fields[2];
    record.firstname = (string)fields[3];
    record.lastname = (string)fields[4];

    // binary field
    record.binarydata = new Binary((byte[])fields[5]);

    ....
}
Armand Jordaan
  • 348
  • 2
  • 11