简体   繁体   English

将字节数组转换为数据表

[英]Convert byte Array into DataTable

I am reading a bin file which contains data in byte format and then converting into DataTable format. 我正在读取一个bin文件,其中包含字节格式的数据,然后转换为DataTable格式。 But the problem is only one row is being read when the file contains 3 rows. 但是问题是当文件包含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(); 
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();
}

This is the working code. 这是工作代码。 It reads all data from file in filestream and then to ByteArray and finally to DataTable. 它从文件流中的文件中读取所有数据,然后读取到ByteArray ,最后读取到DataTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM