使用Filehelpers从数据库中提取记录时,如何处理二进制字段?

时间:2014-01-09 11:56:02

标签: c# sql binary filehelpers

我正在使用Filehelpers从数据库中读取数据。我的代码基于http://filehelpers.sourceforge.net/example_sqlstorage_extract.html

中的示例

我的问题是,根据我的代码,如何处理二进制字段?

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];

    ....
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

为了记录,我设法通过以下方式解决了这个问题(我不确定它是否是最优雅的解决方案,但它对我有用):

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]);

    ....
}