DataTable还是IList

news/2024/7/1 23:20:31 标签: 测试, string, list, exception, table, button

二进制序列化的情况

在远程系统中,经常需要传输集合类型的数据结构,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测试结果

<table style="border-collapse:collapse" border="0">

文件名称

数据量

文件大小

Table100

100

59.9 KB (61,343 字节)

Table1000

1000

583 KB (597,744 字节)

Table10000

10000

5.70 MB (5,979,746 字节)

table>

IList<TestClass>测试结果

<table style="border-collapse:collapse" border="0">

文件名称

数据量

文件大小

List100

100

7.77 KB (7,963 字节)

List1000

1000

72.8 KB (74,563 字节)

List10000

10000

740 KB (758,566 字节)

table>

测试结果可以看出,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测试结果

<table style="border-collapse:collapse" border="0">

文件名称

数据量

文件大小

TableXml100

100

62.7 KB (64,294 字节)

TableXml1000

1000

615 KB (630,395 字节)

TableXml10000

10000

6.01 MB (6,309,396 字节)

table>

IList<TestClass>测试结果

<table style="border-collapse:collapse" border="0">

文件名称

数据量

文件大小

ListXml100

100

46.6 KB (47,741 字节)

ListXml1000

1000

466 KB (477,941 字节)

ListXml10000

10000

4.57 MB (4,797,941 字节)

table>

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

操作性比较

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

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


http://www.niftyadmin.cn/n/1574786.html

相关文章

我的进销存(商贸版)

技术平台&#xff1a;.net 作品介绍&#xff1a;基于remoting技术构建&#xff0c;C/S结构的进销存系统&#xff0c;有进货管理&#xff0c;销售管理&#xff0c;库存管理&#xff0c;财务管理4大模块&#xff0c;能满足中小企使用需要。 主界面 单据 查询 报表 服务器 主要功…

【C#】wpf添加gif动图支持

原文:【C#】wpf添加gif动图支持1.nuget里下载XamlAnimatedGif包&#xff0c;然后安装。 2.添加XamlAnimatedGif包的命名空间&#xff1a;xmlns:gif"https://github.com/XamlAnimatedGif/XamlAnimatedGif" 3.开始使用&#xff1a; <Image gif:AnimationBehavior.So…

餐饮类排队市场“戏路狭小”,美味不用等终难成大气候?

近期&#xff0c;美味不用等获得来自口碑、携程领投的4亿元D1轮融资。此次融资之后&#xff0c;其在市场上的估计达到了40亿人民币。纵观美味不用等的融资历史可以发现&#xff0c;其投资方集结了各路行业巨头&#xff0c;如经纬中国、阿里、腾讯、携程、美团点评等。如此强大的…

我的进销存(工贸版)

技术平台&#xff1a;.net 作品介绍&#xff1a;基于remoting技术构建&#xff0c;C/S结构的进销存系统&#xff0c;有进货管理&#xff0c;销售管理&#xff0c;库存管理&#xff0c;财务管理&#xff0c;生产管理5大模块&#xff0c;能满足中小企使用需要。 主界面 单据 查…

mui 总结

出框框 js内容 mui(".mui-popover").popover(toggle); 点击“弹出框框”就会弹出这个有class"mui-popover"的窗口&#xff0c;点除了窗口的其他位置就隐藏,注意这个窗口一定要写在header之外 此外&#xff0c;mui&#xff08;".classname&qu…

Flex游戏篇——游戏开发概述

概述 游戏开发是个很大的课题&#xff0c;该系列只简单讲述如何使用Flex技术平台搭建一个简单的游戏框架&#xff0c;如何设计能优化游戏的CPU占用&#xff0c;内存等&#xff0c;最后制作一个完整的Flex小游戏(提供源码)。 游戏的呈现 方式1&#xff1a;元件方式 这种方…

OO学习体会与阶段总结(设计与实现)

前言 在最近的一个月的课程中&#xff0c;笔者对于规格化编程进行了深入的学习。运用面向对象抽象思想对编写的程序进行过程抽象、异常处理、数据抽象、类的层次规格与迭代等等规格设计&#xff0c;使得程序结构化程度提高&#xff0c;具有更好的可维护性和复用性。本文通过分析…

我的系统建模工具-EasyStreet 1.0

技术平台&#xff1a;.net 作品介绍&#xff1a;实用的建模工具&#xff0c;能够生成数据层项目代码&#xff0c;生成数据库、数据库脚本&#xff0c;结合内置完善的ORM引擎&#xff0c;能够快速建立管理系统的业务逻辑。输出的数据层可维护性高&#xff0c;扩展容易&#xf…