`
webcenterol
  • 浏览: 914179 次
文章分类
社区版块
存档分类
最新评论

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的协助也能实现强大的查询。

分享到:
评论

相关推荐

    DataTable和IList间转换的封装

    if (row != null) { obj = Activator.CreateInstance(); foreach (DataColumn column in row.Table.Columns) { PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName); ... object value = row...

    C#实现DataTable转换成IList的方法

    本文实例讲述了C#实现DataTable转换成IList的方法。分享给大家供大家参考,具体如下: 在用C#作开发的时候经常要把DataTable转换成IList;操作DataTable比较麻烦,把DataTable转换成IList,以对象实体作为IList的...

    将IList转换成DataTable和DateSet的类

    将IList转换成DataTable和DateSet的类

    IList<T>转换为DataTable

    最近在项目中用到泛型接口通过反射转换为DataTable,查了些资料看到的解决方法不是太对,这里有我解决后的方法,希望对大家有帮助!

    IList转成DataTable和DateSet的类

    IList转换成DataTable IList转换成DateSet 源码 cs文件

    C#实现DataTable,List和Json转换的方法

    1. 将DataTable或Ilist&lt;&gt;转换成JSON格式 using System; using System.Data; using System.Text; using System.Collections.Generic; using System.Reflection; /// /// 将DataTable或Ilist&lt;&gt;转换成JSON格式 ...

    c#将list类型转换成DataTable方法示例

    代码如下:///  /// 将List转换成DataTable ///  /// ”T”&gt; /// ”data”&gt; /// &lt;returns&gt;&lt;/returns&gt; public static DataTable ToDataTable(this IList&lt;T&gt; data) { PropertyDescriptorCollection prope

    ORM及代码生成器C#源码(最新版V4.5.8.5、非常适于ASP.NET MVC)

    public static DataTable ToTable(IList&lt;T&gt; entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static ...

    ORM及代码生成器C#源码(2012最新版Kenly.DBFramework4.6.5.5)

    public static DataTable ToTable(IList&lt;T&gt; entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static ...

    ORM及代码生成器和插件C#源码(DBFrameworkV4.5.3)

    public static DataTable ToTable(IList&lt;T&gt; entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static ...

    datagridview绑定datatable数据,添加合计行,后可排序

    一个简单的datagridview绑定datatable数据,添加合计行,后可排序.

    DevExpress.XtraGrid.GridControl绑定List笔记

    GridControl绑定IList比较简单,但是在调试主从表效果时,一直没能达到预期效果,子列表的列名一直显示相应类的字段名称。这个问题令人大伤脑筋,调试过程着实花了一番功夫,但是问题的解决方法却让人大跌眼镜。 ...

    .Net 面试笔试大全-附答案

    1.new有几种用法 第一种:new Class(); 第二种:覆盖方法 public new XXXX(){} 第三种:new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数。...dataset,datatable,dataview , IList 4.概述反射和序列化

    DataGridView自定义排序

    刚写了一个DataGridView自定义排序的方法,注意,在这个程序中,DataGridView的DataSource不是DataTable ,而是IList集合,加载到DataGridView里全是string类型,在既有文字和数字的地方要进行不同的排序,特别是...

    使用泛型将数据表转换为集合

    从DataTable到T.// DataTable数组:从IListpublic静态DataTable ToDataTable(this IList iList){DataTable dataTable = new DataTable(); PropertyDescriptorCollection propertyDescriptorCollection = ...

    asp.net面试试题收集

    3.datagrid.datasouse可以连接什么数据源 [dataset,datatable,dataview] &lt;br&gt;dataset,datatable,dataview , IList &lt;br&gt;4.概述反射和序列化 &lt;br&gt;反射:程序集包含模块,而模块包含类型,类型又包含成员。反射...

    NpoiHelper

    //将获取到的供货商IList集合转换为DataTable类型 DataTable dt = NpoiHelper.ToDataTable(list); MemoryStream ms = new MemoryStream(); //将数据导出到Excel中 if (dt.Rows.Count &gt; 0) { ...

    WinForm中comboBox控件数据绑定实现方法

    下面介绍三种对comboBox绑定的方式,分别是泛型中IList和Dictionary,还有数据集DataTable  一、IList 现在我们直接创建一个List集合,然后绑定 IList&lt;string&gt; list = new List(); list.Add(111111); list.Add...

Global site tag (gtag.js) - Google Analytics