C# 使用Filehelpers从数据库提取记录时,如何处理二进制字段?
我正在使用Filehelpers从数据库读取数据。我的代码基于以下示例: 我的问题是,给定我的代码,如何处理二进制字段C# 使用Filehelpers从数据库提取记录时,如何处理二进制字段?,c#,sql,binary,filehelpers,C#,Sql,Binary,Filehelpers,我正在使用Filehelpers从数据库读取数据。我的代码基于以下示例: 我的问题是,给定我的代码,如何处理二进制字段 public class StudentRecord { public string registration_id; public string student_number; public string card_number; public string firstname; public string lastname;
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];
....
}
任何帮助都将不胜感激。作为记录,我通过以下方式解决了这个问题(我不确定这是否是最优雅的解决方案,但它确实对我有效):
不知道什么是
Filehelpers
,但它应该以某种方式表示数据(字段[5]
)。如果它是byte[]
,那么您的BinaryData
应该有一个构造函数/方法从中初始化。
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]);
....
}