DataTable还是IList

二进制序列化的情况

在远程系统中,经常需要传输集合类型的数据结构,DataTable和IList<T>是比较常用的2种集合类型,下面对这2种数据类型的二进制序列化作一个测试

定义一个测试的类

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

namespace WinTest

{

[Serializable]

public class TestClass

{

public TestClass()

{

//1个单位的数据量如下

Col1 = "普通随碟附送234124323";

Col2 = "普通随碟附送234";

Col3 = "普通随碟附送sdfsaf";

Col4 = "普通随碟附送sdfs";

Col5 = "普通随碟附送3235";

Col6 = "普通随碟附sadfw;eois;lkapwoeritypeoy340563496uepryupoew9u70463096uoe45iu645oi6u4o5i6u4o5i6uwo45iu6wo5u6送3235";

Col7 = 123.54M;

Col8 = 123.54M;

Col9 = 123.54M;

Col10 = DateTime.Now;

}

public string Col1 { get; set; }

public string Col2 { get; set; }

public string Col3 { get; set; }

public string Col4 { get; set; }

public string Col5 { get; set; }

public string Col6 { get; set; }

public decimal Col7 { get; set; }

public decimal Col8 { get; set; }

public decimal Col9 { get; set; }

public DateTime Col10 { get; set; }

//创建测试的DataTable

public static DataTable CreateTable(int count)

{

DataTable dt = new DataTable();

dt.TableName = "DataTable";

dt.Columns.AddRange(new DataColumn[] {

new DataColumn("Col1",typeof(string)),

new DataColumn("Col2",typeof(string)),

new DataColumn("Col3",typeof(string)),

new DataColumn("Col4",typeof(string)),

new DataColumn("Col5",typeof(string)),

new DataColumn("Col6",typeof(string)),

new DataColumn("Col7",typeof(decimal)),

new DataColumn("Col8",typeof(decimal)),

new DataColumn("Col9",typeof(decimal)),

new DataColumn("Col10",typeof(DateTime)),

});

for (int i = 0; i < count; i++)

{

DataRow row = dt.NewRow();

TestClass test = new TestClass();

row["Col1"] = test.Col1;

row["Col2"] = test.Col2;

row["Col3"] = test.Col3;

row["Col4"] = test.Col4;

row["Col5"] = test.Col5;

row["Col6"] = test.Col6;

row["Col7"] = test.Col7;

row["Col8"] = test.Col8;

row["Col9"] = test.Col9;

row["Col10"] = test.Col10;

dt.Rows.Add(row);

}

return dt;

}

//创建测试的IList<T>

public static List<TestClass> CreateList(int count)

{

List<TestClass> list = new List<TestClass>();

for (int i = 0; i < count; i++)

{

TestClass test = new TestClass();

list.Add(test);

}

return list;

}

}

}

窗体测试代码如下:

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

private void button1_Click(object sender, EventArgs e)

{

try

{

BinaryFormatter bin = new BinaryFormatter();

//100行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "List100"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateList(100));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "Table100"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateTable(100));

fs.Close();

}

//1000行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "List1000"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateList(1000));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "Table1000"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateTable(1000));

fs.Close();

}

//10000行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "List10000"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateList(10000));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "Table10000"), FileMode.Create))

{

bin.Serialize(fs, TestClass.CreateTable(10000));

fs.Close();

}

}

catch (Exception ex)

{

}

}

DataTable测试结果

文件名称

数据量

文件大小

Table100

100

59.9 KB (61,343 字节)

Table1000

1000

583 KB (597,744 字节)

Table10000

10000

5.70 MB (5,979,746 字节)

IList<TestClass>测试结果

文件名称

数据量

文件大小

List100

100

7.77 KB (7,963 字节)

List1000

1000

72.8 KB (74,563 字节)

List10000

10000

740 KB (758,566 字节)

从测试结果可以看出,IList<T>序列化的文件大小比DataTable小得多,这意味着在数据传输中带宽占用小很多,所以在设计Remoting接口时尽量使用IList<T>作返回值。

XML序列化的情况

窗体测试代码如下:

using System.Xml.Serialization;

using System.IO;

private void button1_Click(object sender, EventArgs e)

{

try

{

XmlSerializer listSer = new XmlSerializer(typeof(List<TestClass>));

XmlSerializer tableSer = new XmlSerializer(typeof(DataTable));

//100行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "ListXml100"), FileMode.Create))

{

listSer.Serialize(fs, TestClass.CreateList(100));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "TableXml100"), FileMode.Create))

{

tableSer.Serialize(fs, TestClass.CreateTable(100));

fs.Close();

}

//1000行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "ListXml1000"), FileMode.Create))

{

listSer.Serialize(fs, TestClass.CreateList(1000));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "TableXml1000"), FileMode.Create))

{

tableSer.Serialize(fs, TestClass.CreateTable(1000));

fs.Close();

}

//10000行

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "ListXml10000"), FileMode.Create))

{

listSer.Serialize(fs, TestClass.CreateList(10000));

fs.Close();

}

using (FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "TableXml10000"), FileMode.Create))

{

tableSer.Serialize(fs, TestClass.CreateTable(10000));

fs.Close();

}

}

catch (Exception ex)

{

}

}

DataTable测试结果

文件名称

数据量

文件大小

TableXml100

100

62.7 KB (64,294 字节)

TableXml1000

1000

615 KB (630,395 字节)

TableXml10000

10000

6.01 MB (6,309,396 字节)

IList<TestClass>测试结果

文件名称

数据量

文件大小

ListXml100

100

46.6 KB (47,741 字节)

ListXml1000

1000

466 KB (477,941 字节)

ListXml10000

10000

4.57 MB (4,797,941 字节)

从测试结果可以看出,IList<T>序列化后的文件比同样比DataTable小,但差距已经没有二进制序列化那么明显了。而且IList<T>的二进制序列化和XML序列化相差很大,所以remoteing中建议使用二进制序列化。

操作性比较

DataTable有支持数据的提交、回滚、查询等强大的方法,但访问单元格内容的时候不方便,还要类型转换。

IList<T>则访问项的属性比较方便,有属性自动提示,不用类型转换,有LINQ的协助也能实现强大的查询。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值