110,536
社区成员
发帖
与我相关
我的任务
分享
protected void Serialize(int rowCount)
{
Console.WriteLine("时间:{0:yyyy-MM-dd HH:mm:ss ddd}, 序列化数据, 共{1}行.", DateTime.Now, rowCount);
//创建数据源
DataTable dt = new DataTable();
for (int index = 0; index < 100; index++)
{
dt.Columns.Add(string.Format("Col{0}", index), typeof(string));
}
for (int j = 0; j < 2e4; j++)
{
DataRow newRow = dt.NewRow();
for (int index = 0; index < 100; index++)
{
newRow[string.Format("Col{0}", index)] = string.Format("数值({0}) 加 数字({1}) 等于 值({2})", index, j, index + j);
}
dt.Rows.Add(newRow);
}
Console.WriteLine("时间:{0:yyyy-MM-dd HH:mm:ss ddd}, 已建立数据源.", DateTime.Now);
//序列化
MemoryStream stream = new MemoryStream();
new BinaryFormatter().Serialize(stream, dt);
Console.WriteLine("时间:{0:yyyy-MM-dd HH:mm:ss ddd}, 流长度{1:#,###}, 类型DataTable.", DateTime.Now, stream.Length);
//序列化
object obj = dt as object;
new BinaryFormatter().Serialize(stream, obj);
Console.WriteLine("时间:{0:yyyy-MM-dd HH:mm:ss ddd}, 流长度{1:#,###}, 类型Object.", DateTime.Now, stream.Length);
}