将字节数组转换为DataTable

时间:2014-10-22 09:11:28

标签: c# datatable bytearray binaryformatter

我正在读取一个bin文件,其中包含字节格式的数据,然后转换为DataTable格式。但问题是当文件包含3行时只读取一行。

byte[] bytedata = File.ReadAllBytes("Output1.bin");
DataTable data = new DataTable();
BinaryFormatter bformatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream = new MemoryStream(bytedata);
data = (DataTable)bformatter.Deserialize(stream);
stream.Close(); 

1 个答案:

答案 0 :(得分:1)

const int MAX_BUFFER = 2048;
byte[] Buffer = new byte[MAX_BUFFER];
int BytesRead;
DataTable data = new DataTable();
using (System.IO.FileStream fileStream = new FileStream("Output1.bin", FileMode.Open, FileAccess.Read))
while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
{
string text = (Convert.ToBase64String(Buffer));
BinaryFormatter bformatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream = new MemoryStream(Buffer);
data = (DataTable)bformatter.Deserialize(stream);
stream.Close();
}

这是工作代码。它从文件流中的文件读取所有数据,然后读取到ByteArray,最后读取到DataTable。