如何将DataTableReader对象转换为bytes数组?

时间:2015-11-23 04:19:33

标签: c# .net

我从数据库和本地XML文件中获取数据。我使用MySqlReader通过select查询从Database获取数据,并使用DataTableReader对象从XML获取数据。

MySqlReader可以转换字节数组,但DataTableReader不能转换为字节数组。

//following block is working fine
MySqlDataReader mySqlReader = new MySqlDataReader();
mySqlReader = it will get data from database by select query

Employee employee = new Employee();
employee.Image = DevExpress.XtraEditors.Controls.ByteImageConverter.FromByteArray((byte[])mySqlReader["profilePic"]); 

//following block is not working. Error : "unable to cast object of type 'System.String' to type 'System.Bytes[]'"
DataTable dtEmployee = new DataTable();
dtEmployee = dataset.Tables[0];
DataTableReader dtReader = dtEmployee.CreateDataReader();

Employee employee = new Employee();
//error throws here
employee.Image = DevExpress.XtraEditors.Controls.ByteImageConverter.FromByteArray((byte[])dtReader["profilePic"]); 

有人可以建议如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你的MySQL表可能有'ProfilePic'的BLOB类型,因此它可以被转换为字节数组,但是,二进制数据如何存储在XML中?您的错误表明它存储为字符串,可能在Base64中,因此请尝试使用Convert.FromBase64String

employee.Image = DevExpress.XtraEditors.Controls.ByteImageConverter.FromByteArray((byte[])Convert.FromBase64String(dtReader["profilePic"]));

如果您的二进制数据以其他格式存储,则需要另一个转换器......