在最常见的用法中,从Reader到Entity或Collection或List时总会用到对象转化。
这里提供一份简陋的性能测试报告(仅代表downmoon或邀月)的观点。
一、在测试之前,首先准备一个表(SQL2005)
Database Name :TestBigData
Table:Product
这里,提供一些傻瓜语句
create database Testbigdata go use Testbigdata GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Product]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[Product]( [P_ID] [bigint] IDENTITY(1,1) NOT NULL, [P_Name] [nvarchar](255) NULL, [P_E_ID] [int] NULL, [CategoryID1] [int] NULL CONSTRAINT [DF__Product__Categor__04AFB25B] DEFAULT ((0)), [CategoryID2] [int] NULL CONSTRAINT [DF__Product__Categor__05A3D694] DEFAULT ((0)), [CategoryID3] [int] NULL CONSTRAINT [DF__Product__Categor__0697FACD] DEFAULT ((0)), [CategoryID4] [int] NULL CONSTRAINT [DF__Product__Categor__078C1F06] DEFAULT ((0)), [CategoryID5] [int] NULL CONSTRAINT [DF__Product__Categor__0880433F] DEFAULT ((0)), [CategoryID6] [int] NULL CONSTRAINT [DF__Product__Categor__09746778] DEFAULT ((0)), [P_SysCate] [nvarchar](20) NULL, [P_Key] [nvarchar](100) NULL, [P_SellArea] [nvarchar](500) NULL, [P_SingleIntro] [nvarchar](1000) NULL, [P_Intro] [ntext] NULL, [P_Order] [float] NULL, [P_Cate] [nvarchar](20) NULL, [P_Cate1] [int] NULL, [P_Cate2] [int] NULL, [P_Cate3] [int] NULL, [P_C_Order] [int] NULL, [P_TopTime] [smalldatetime] NULL, [AddTime] [datetime] NULL, [AddUser] [nvarchar](50) NULL, [ModTime] [datetime] NULL, [ModUser] [nvarchar](50) NULL, [P_BigImage] [nvarchar](150) NULL, [P_SmallImage] [nvarchar](150) NULL, [P_BigImage2] [nvarchar](150) NULL, [P_SmallImage2] [nvarchar](150) NULL, [P_BigImage3] [nvarchar](150) NULL, [P_SmallImage3] [nvarchar](150) NULL, [LoginID] [nvarchar](50) NULL, [CurState] [smallint] NOT NULL CONSTRAINT [DF__Product__CurStat__0A688BB1] DEFAULT ((0)), [RecState] [smallint] NOT NULL CONSTRAINT [DF_Product_RecCtate] DEFAULT ((0)), [P_CheckInfo] [nvarchar](80) NULL, [P_Max] [nvarchar](30) NULL, [P_Min] [nvarchar](30) NULL, [P_Unit] [nvarchar](50) NULL, [P_L_ID] [int] NULL, [C_ID] [int] NULL, [C_I_ID] [int] NULL CONSTRAINT [DF__Product__C_I_ID__0B5CAFEA] DEFAULT ((0)), [P_NewInfo] [nvarchar](1000) NULL, [P_Price] [nvarchar](50) NULL, [P_OtherState] [int] NULL, [P_NewKey1] [nvarchar](300) NULL, [P_NewKey2] [nvarchar](300) NULL, [P_NewKey3] [nvarchar](300) NULL, [F1] [int] NOT NULL CONSTRAINT [DF__Product__F1__0C50D423] DEFAULT ((0)), [F2] [int] NOT NULL CONSTRAINT [DF__Product__F2__0D44F85C] DEFAULT ((0)), [F3] [nvarchar](50) NULL, [F4] [nvarchar](200) NULL, [WebSite] [nvarchar](50) NULL, [Languages] [nvarchar](20) NULL, CONSTRAINT [PK_PRODUCT] PRIMARY KEY CLUSTERED ( [P_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] END SET Identity_Insert dbo.Product ON INSERT INTO dbo.Product( P_ID,P_Name,P_E_ID,CategoryID1, CategoryID2,CategoryID3,CategoryID4,CategoryID5, CategoryID6,P_SysCate,P_Key,P_SellArea, P_SingleIntro,P_Intro,P_Order,P_Cate, P_Cate1,P_Cate2,P_Cate3,P_C_Order, P_TopTime,AddTime,AddUser,ModTime, ModUser,P_BigImage,P_SmallImage,P_BigImage2, P_SmallImage2,P_BigImage3,P_SmallImage3,LoginID, CurState,RecState,P_CheckInfo,P_Max, P_Min,P_Unit,P_L_ID,C_ID, C_I_ID,P_NewInfo,P_Price,P_OtherState, P_NewKey1,P_NewKey2,P_NewKey3,F1, F2,F3,F4,WebSite, Languages) VALUES ( 262144,'成功1号',0, 4,9,332,0, 0,0,'花椰菜','花菜种子,花椰菜种子,白花菜种子', '1','花椰菜种子,特征特性:一般株高65厘米,开展度65-70厘米,叶深绿色,长椭圆形,蜡粉中等,叶柄短,','<P>特征特性:一般株高65厘米,开展度65-70厘米,叶深绿色,长椭圆形,蜡粉中等,叶柄短,脚特短,节间特别紧密,芯叶多层扭卷护球。部份叶柄嵌入花球下方,使花球成圆球形,花球三叠,特别洁白坚实紧密,质地柔嫩,鲜美可口,比重特大。一般单花球重2-3千克左右,适应性广,耐运输,还是冷冻出口创汇的好原料。元旦前上市。结球期适应温度 7-10℃。</P> <P> 栽培要点:该品种适宜北纬 31℃以内种植。浙江省一般7月下旬播种为宜,其它地区敬请参照当地气候与栽培习惯,慎选最佳播期。一般南稍迟、北稍早;沿海平原稍迟、内陆山区稍早。苗期适当遮荫,搭架避雨,苗龄25-30天(包括假植时间)。合理密植,每亩1400-1500株。施足基肥,成活后淡肥勤施,花球形成后(时)重施追肥 2-3次。及时摘叶盖花,生长期间要防治病虫害和结球期鼠害。 <BR> 播种期间不适,苗期过长,肥水管理跟不上,遇到天气长期高温晴旱或暴雨淋伤根系都会提早形成花球,球小品质差。严重时容易出现毛花、异花(畸形花)。要照样施肥水,有利于增加产量,提高质量。</P>',0, 'Seed',20,0,0, 0,'2008-02-28 00:00:00.000','2004-10-09 17:37:00.000',NULL, '2008-03-13 19:35:59.140','qbd','20064201105007754.jpg','20064201105004166.jpg', '','','','', 'qingyiseeds',1,0,'', '','','千克/公斤',0, 10633,0,'','', 0,'花菜种子,花椰菜种子,白花菜种子','','', 0,0,'','', 'agronet','cn') GO Set Identity_Insert dbo.Product OFF
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
create
database
Testbigdata
go
use
Testbigdata
GO



SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
IF
NOT
EXISTS
(
SELECT
*
FROM
sys.objects
WHERE
object_id
=
OBJECT_ID
(N
'
[dbo].[Product]
'
)
AND
type
in
(N
'
U
'
))
BEGIN
CREATE
TABLE
[
dbo
]
.
[
Product
]
(
[
P_ID
]
[
bigint
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
P_Name
]
[
nvarchar
]
(
255
)
NULL
,
[
P_E_ID
]
[
int
]
NULL
,
[
CategoryID1
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__04AFB25B
]
DEFAULT
((
0
)),
[
CategoryID2
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__05A3D694
]
DEFAULT
((
0
)),
[
CategoryID3
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__0697FACD
]
DEFAULT
((
0
)),
[
CategoryID4
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__078C1F06
]
DEFAULT
((
0
)),
[
CategoryID5
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__0880433F
]
DEFAULT
((
0
)),
[
CategoryID6
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__Categor__09746778
]
DEFAULT
((
0
)),
[
P_SysCate
]
[
nvarchar
]
(
20
)
NULL
,
[
P_Key
]
[
nvarchar
]
(
100
)
NULL
,
[
P_SellArea
]
[
nvarchar
]
(
500
)
NULL
,
[
P_SingleIntro
]
[
nvarchar
]
(
1000
)
NULL
,
[
P_Intro
]
[
ntext
]
NULL
,
[
P_Order
]
[
float
]
NULL
,
[
P_Cate
]
[
nvarchar
]
(
20
)
NULL
,
[
P_Cate1
]
[
int
]
NULL
,
[
P_Cate2
]
[
int
]
NULL
,
[
P_Cate3
]
[
int
]
NULL
,
[
P_C_Order
]
[
int
]
NULL
,
[
P_TopTime
]
[
smalldatetime
]
NULL
,
[
AddTime
]
[
datetime
]
NULL
,
[
AddUser
]
[
nvarchar
]
(
50
)
NULL
,
[
ModTime
]
[
datetime
]
NULL
,
[
ModUser
]
[
nvarchar
]
(
50
)
NULL
,
[
P_BigImage
]
[
nvarchar
]
(
150
)
NULL
,
[
P_SmallImage
]
[
nvarchar
]
(
150
)
NULL
,
[
P_BigImage2
]
[
nvarchar
]
(
150
)
NULL
,
[
P_SmallImage2
]
[
nvarchar
]
(
150
)
NULL
,
[
P_BigImage3
]
[
nvarchar
]
(
150
)
NULL
,
[
P_SmallImage3
]
[
nvarchar
]
(
150
)
NULL
,
[
LoginID
]
[
nvarchar
]
(
50
)
NULL
,
[
CurState
]
[
smallint
]
NOT
NULL
CONSTRAINT
[
DF__Product__CurStat__0A688BB1
]
DEFAULT
((
0
)),
[
RecState
]
[
smallint
]
NOT
NULL
CONSTRAINT
[
DF_Product_RecCtate
]
DEFAULT
((
0
)),
[
P_CheckInfo
]
[
nvarchar
]
(
80
)
NULL
,
[
P_Max
]
[
nvarchar
]
(
30
)
NULL
,
[
P_Min
]
[
nvarchar
]
(
30
)
NULL
,
[
P_Unit
]
[
nvarchar
]
(
50
)
NULL
,
[
P_L_ID
]
[
int
]
NULL
,
[
C_ID
]
[
int
]
NULL
,
[
C_I_ID
]
[
int
]
NULL
CONSTRAINT
[
DF__Product__C_I_ID__0B5CAFEA
]
DEFAULT
((
0
)),
[
P_NewInfo
]
[
nvarchar
]
(
1000
)
NULL
,
[
P_Price
]
[
nvarchar
]
(
50
)
NULL
,
[
P_OtherState
]
[
int
]
NULL
,
[
P_NewKey1
]
[
nvarchar
]
(
300
)
NULL
,
[
P_NewKey2
]
[
nvarchar
]
(
300
)
NULL
,
[
P_NewKey3
]
[
nvarchar
]
(
300
)
NULL
,
[
F1
]
[
int
]
NOT
NULL
CONSTRAINT
[
DF__Product__F1__0C50D423
]
DEFAULT
((
0
)),
[
F2
]
[
int
]
NOT
NULL
CONSTRAINT
[
DF__Product__F2__0D44F85C
]
DEFAULT
((
0
)),
[
F3
]
[
nvarchar
]
(
50
)
NULL
,
[
F4
]
[
nvarchar
]
(
200
)
NULL
,
[
WebSite
]
[
nvarchar
]
(
50
)
NULL
,
[
Languages
]
[
nvarchar
]
(
20
)
NULL
,
CONSTRAINT
[
PK_PRODUCT
]
PRIMARY
KEY
CLUSTERED
(
[
P_ID
]
ASC
)
WITH
(PAD_INDEX
=
OFF
,STATISTICS_NORECOMPUTE
=
OFF
,IGNORE_DUP_KEY
=
OFF
,ALLOW_ROW_LOCKS
=
ON
,ALLOW_PAGE_LOCKS
=
ON
)
ON
[
PRIMARY
]
)
ON
[
PRIMARY
]
TEXTIMAGE_ON
[
PRIMARY
]
END

SET
Identity_Insert
dbo.Product
ON
INSERT
INTO
dbo.Product(
P_ID,P_Name,P_E_ID,CategoryID1,
CategoryID2,CategoryID3,CategoryID4,CategoryID5,
CategoryID6,P_SysCate,P_Key,P_SellArea,
P_SingleIntro,P_Intro,P_Order,P_Cate,
P_Cate1,P_Cate2,P_Cate3,P_C_Order,
P_TopTime,AddTime,AddUser,ModTime,
ModUser,P_BigImage,P_SmallImage,P_BigImage2,
P_SmallImage2,P_BigImage3,P_SmallImage3,LoginID,
CurState,RecState,P_CheckInfo,P_Max,
P_Min,P_Unit,P_L_ID,C_ID,
C_I_ID,P_NewInfo,P_Price,P_OtherState,
P_NewKey1,P_NewKey2,P_NewKey3,F1,
F2,F3,F4,WebSite,
Languages)
VALUES
(
262144
,
'
成功1号
'
,
0
,
4
,
9
,
332
,
0
,
0
,
0
,
'
花椰菜
'
,
'
花菜种子,花椰菜种子,白花菜种子
'
,
'
1
'
,
'
花椰菜种子,特征特性:一般株高65厘米,开展度65-70厘米,叶深绿色,长椭圆形,蜡粉中等,叶柄短,
'
,
'
<P>
特征特性:一般株高65厘米,开展度65-70厘米,叶深绿色,长椭圆形,蜡粉中等,叶柄短,脚特短,节间特别紧密,芯叶多层扭卷护球。部份叶柄嵌入花球
下方,使花球成圆球形,花球三叠,特别洁白坚实紧密,质地柔嫩,鲜美可口,比重特大。一般单花球重2-3千克左右,适应性广,耐运输,还是冷冻出口创汇的
好原料。元旦前上市。结球期适应温度7-10℃。</P>
<P> 栽
培要点:该品种适宜北纬31℃以内种植。浙江省一般7月下旬播种为宜,其它地区敬请参照当地气候与栽培习惯,慎选最佳播期。一般南稍迟、北稍早;沿海平原
稍迟、内陆山区稍早。苗期适当遮荫,搭架避雨,苗龄25-30天(包括假植时间)。合理密植,每亩1400-1500株。施足基肥,成活后淡肥勤施,花球
形成后(时)重施追肥2-3次。及时摘叶盖花,生长期间要防治病虫害和结球期鼠害。
<BR> 播种期间不适,苗期过长,肥水管理跟不上,遇到天气长期高温晴旱或
暴雨淋伤根系都会提早形成花球,球小品质差。严重时容易出现毛花、异花(畸形花)。要照样施肥水,有利于增加产量,提高质量。</P>
'
,
0
,
'
Seed
'
,
20
,
0
,
0
,
0
,
'
2008-02-2800:00:00.000
'
,
'
2004-10-0917:37:00.000
'
,
NULL
,
'
2008-03-1319:35:59.140
'
,
'
qbd
'
,
'
20064201105007754.jpg
'
,
'
20064201105004166.jpg
'
,
''
,
''
,
''
,
''
,
'
qingyiseeds
'
,
1
,
0
,
''
,
''
,
''
,
'
千克/公斤
'
,
0
,
10633
,
0
,
''
,
''
,
0
,
'
花菜种子,花椰菜种子,白花菜种子
'
,
''
,
''
,
0
,
0
,
''
,
''
,
'
agronet
'
,
'
cn
'
)
GO
Set
Identity_Insert
dbo.Product
OFF
假定己插入2048条记录
二、创建ConSole Application
主要三个类如下:
最常见的用法:
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
using
System;
using
System.Data.SqlClient;
namespace
DownmoonTestConsole
{
public
class
ManualBuilder
{
public
Agronet.Product.Framework.Components.ProductBuild(SqlDataReaderreader)
{
Agronet.Product.Framework.Components.Productm_Product
=
new
Agronet.Product.Framework.Components.Product();
if
(reader[
"
P_ID
"
]
!=
DBNull.Value)
{m_Product.P_ID
=
Convert.ToInt64(reader[
"
P_ID
"
]);}

if
(reader[
"
P_Name
"
]
!=
DBNull.Value)
{m_Product.P_Name
=
Convert.ToString(reader[
"
P_Name
"
]);}

if
(reader[
"
P_E_ID
"
]
!=
DBNull.Value)
{m_Product.P_E_ID
=
Convert.ToInt32(reader[
"
P_E_ID
"
]);}

if
(reader[
"
CategoryID1
"
]
!=
DBNull.Value)
{m_Product.CategoryID1
=
Convert.ToInt32(reader[
"
CategoryID1
"
]);}

if
(reader[
"
CategoryID2
"
]
!=
DBNull.Value)
{m_Product.CategoryID2
=
Convert.ToInt32(reader[
"
CategoryID2
"
]);}

if
(reader[
"
CategoryID3
"
]
!=
DBNull.Value)
{m_Product.CategoryID3
=
Convert.ToInt32(reader[
"
CategoryID3
"
]);}

if
(reader[
"
CategoryID4
"
]
!=
DBNull.Value)
{m_Product.CategoryID4
=
Convert.ToInt32(reader[
"
CategoryID4
"
]);}

if
(reader[
"
CategoryID5
"
]
!=
DBNull.Value)
{m_Product.CategoryID5
=
Convert.ToInt32(reader[
"
CategoryID5
"
]);}

if
(reader[
"
CategoryID6
"
]
!=
DBNull.Value)
{m_Product.CategoryID6
=
Convert.ToInt32(reader[
"
CategoryID6
"
]);}

if
(reader[
"
P_SysCate
"
]
!=
DBNull.Value)
{m_Product.P_SysCate
=
Convert.ToString(reader[
"
P_SysCate
"
]);}

if
(reader[
"
P_Key
"
]
!=
DBNull.Value)
{m_Product.P_Key
=
Convert.ToString(reader[
"
P_Key
"
]);}

if
(reader[
"
P_SellArea
"
]
!=
DBNull.Value)
{m_Product.P_SellArea
=
Convert.ToString(reader[
"
P_SellArea
"
]);}

if
(reader[
"
P_SingleIntro
"
]
!=
DBNull.Value)
{m_Product.P_SingleIntro
=
Convert.ToString(reader[
"
P_SingleIntro
"
]);}

if
(reader[
"
P_Intro
"
]
!=
DBNull.Value)
{m_Product.P_Intro
=
Convert.ToString(reader[
"
P_Intro
"
]);}

if
(reader[
"
P_Order
"
]
!=
DBNull.Value)
{m_Product.P_Order
=
Convert.ToDouble(reader[
"
P_Order
"
]);}

if
(reader[
"
P_Cate
"
]
!=
DBNull.Value)
{m_Product.P_Cate
=
Convert.ToString(reader[
"
P_Cate
"
]);}

if
(reader[
"
P_Cate1
"
]
!=
DBNull.Value)
{m_Product.P_Cate1
=
Convert.ToInt32(reader[
"
P_Cate1
"
]);}

if
(reader[
"
P_Cate2
"
]
!=
DBNull.Value)
{m_Product.P_Cate2
=
Convert.ToInt32(reader[
"
P_Cate2
"
]);}

if
(reader[
"
P_Cate3
"
]
!=
DBNull.Value)
{m_Product.P_Cate3
=
Convert.ToInt32(reader[
"
P_Cate3
"
]);}

if
(reader[
"
P_C_Order
"
]
!=
DBNull.Value)
{m_Product.P_C_Order
=
Convert.ToInt32(reader[
"
P_C_Order
"
]);}

if
(reader[
"
P_TopTime
"
]
!=
DBNull.Value)
{m_Product.P_TopTime
=
Convert.ToDateTime(reader[
"
P_TopTime
"
]);}

if
(reader[
"
P_BigImage
"
]
!=
DBNull.Value)
{m_Product.P_BigImage
=
Convert.ToString(reader[
"
P_BigImage
"
]);}

if
(reader[
"
P_SmallImage
"
]
!=
DBNull.Value)
{m_Product.P_SmallImage
=
Convert.ToString(reader[
"
P_SmallImage
"
]);}

if
(reader[
"
P_BigImage2
"
]
!=
DBNull.Value)
{m_Product.P_BigImage2
=
Convert.ToString(reader[
"
P_BigImage2
"
]);}

if
(reader[
"
P_SmallImage2
"
]
!=
DBNull.Value)
{m_Product.P_SmallImage2
=
Convert.ToString(reader[
"
P_SmallImage2
"
]);}

if
(reader[
"
P_BigImage3
"
]
!=
DBNull.Value)
{m_Product.P_BigImage3
=
Convert.ToString(reader[
"
P_BigImage3
"
]);}

if
(reader[
"
P_SmallImage3
"
]
!=
DBNull.Value)
{m_Product.P_SmallImage3
=
Convert.ToString(reader[
"
P_SmallImage3
"
]);}

if
(reader[
"
LoginID
"
]
!=
DBNull.Value)
{m_Product.LoginID
=
Convert.ToString(reader[
"
LoginID
"
]);}

if
(reader[
"
CurState
"
]
!=
DBNull.Value)
{m_Product.CurState
=
Convert.ToInt16(reader[
"
CurState
"
]);}

if
(reader[
"
RecState
"
]
!=
DBNull.Value)
{m_Product.RecState
=
Convert.ToInt16(reader[
"
RecState
"
]);}

if
(reader[
"
P_CheckInfo
"
]
!=
DBNull.Value)
{m_Product.P_CheckInfo
=
Convert.ToString(reader[
"
P_CheckInfo
"
]);}

if
(reader[
"
P_Max
"
]
!=
DBNull.Value)
{m_Product.P_Max
=
Convert.ToString(reader[
"
P_Max
"
]);}

if
(reader[
"
P_Min
"
]
!=
DBNull.Value)
{m_Product.P_Min
=
Convert.ToString(reader[
"
P_Min
"
]);}

if
(reader[
"
P_Unit
"
]
!=
DBNull.Value)
{m_Product.P_Unit
=
Convert.ToString(reader[
"
P_Unit
"
]);}

if
(reader[
"
P_L_ID
"
]
!=
DBNull.Value)
{m_Product.P_L_ID
=
Convert.ToInt32(reader[
"
P_L_ID
"
]);}

if
(reader[
"
C_ID
"
]
!=
DBNull.Value)
{m_Product.C_ID
=
Convert.ToInt32(reader[
"
C_ID
"
]);}

if
(reader[
"
C_I_ID
"
]
!=
DBNull.Value)
{m_Product.C_I_ID
=
Convert.ToInt32(reader[
"
C_I_ID
"
]);}

if
(reader[
"
P_NewInfo
"
]
!=
DBNull.Value)
{m_Product.P_NewInfo
=
Convert.ToString(reader[
"
P_NewInfo
"
]);}

if
(reader[
"
P_Price
"
]
!=
DBNull.Value)
{m_Product.P_Price
=
Convert.ToString(reader[
"
P_Price
"
]);}

if
(reader[
"
P_OtherState
"
]
!=
DBNull.Value)
{m_Product.P_OtherState
=
Convert.ToInt32(reader[
"
P_OtherState
"
]);}

if
(reader[
"
P_NewKey1
"
]
!=
DBNull.Value)
{m_Product.P_NewKey1
=
Convert.ToString(reader[
"
P_NewKey1
"
]);}

if
(reader[
"
P_NewKey2
"
]
!=
DBNull.Value)
{m_Product.P_NewKey2
=
Convert.ToString(reader[
"
P_NewKey2
"
]);}

if
(reader[
"
P_NewKey3
"
]
!=
DBNull.Value)
{m_Product.P_NewKey3
=
Convert.ToString(reader[
"
P_NewKey3
"
]);}

if
(reader[
"
AddTime
"
]
!=
DBNull.Value)
{m_Product.AddTime
=
Convert.ToDateTime(reader[
"
AddTime
"
]);}

if
(reader[
"
AddUser
"
]
!=
DBNull.Value)
{m_Product.AddUser
=
Convert.ToString(reader[
"
AddUser
"
]);}

if
(reader[
"
ModTime
"
]
!=
DBNull.Value)
{m_Product.ModTime
=
Convert.ToDateTime(reader[
"
ModTime
"
]);}

if
(reader[
"
ModUser
"
]
!=
DBNull.Value)
{m_Product.ModUser
=
Convert.ToString(reader[
"
ModUser
"
]);}

if
(reader[
"
F1
"
]
!=
DBNull.Value)
{m_Product.F1
=
Convert.ToInt32(reader[
"
F1
"
]);}

if
(reader[
"
F2
"
]
!=
DBNull.Value)
{m_Product.F2
=
Convert.ToInt32(reader[
"
F2
"
]);}

if
(reader[
"
F3
"
]
!=
DBNull.Value)
{m_Product.F3
=
Convert.ToString(reader[
"
F3
"
]);}

if
(reader[
"
F4
"
]
!=
DBNull.Value)
{m_Product.F4
=
Convert.ToString(reader[
"
F4
"
]);}

if
(reader[
"
WebSite
"
]
!=
DBNull.Value)
{m_Product.WebSite
=
Convert.ToString(reader[
"
WebSite
"
]);}

if
(reader[
"
Languages
"
]
!=
DBNull.Value)
{m_Product.Languages
=
Convert.ToString(reader[
"
Languages
"
]);}

return
m_Product;
}
}
}
第二种用法(Reflect):
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
using
System;
using
System.Data.SqlClient;
using
System.Reflection;
namespace
DynamicMappingSpike
{
public
class
ReflectionBuilder
<
T
>

{
private
PropertyInfo[]properties;

private
ReflectionBuilder()
{}

public
TBuild(SqlDataReaderreader)
{
Tresult
=
(T)Activator.CreateInstance(
typeof
(T));
for
(
int
i
=
0
;i
<
reader.FieldCount;i
++
)
{
if
(properties[i]
!=
null
&&
!
reader.IsDBNull(i))
{
properties[i].SetValue(result,reader[i],
null
);
}
}

return
result;
}

public
static
ReflectionBuilder
<
T
>
CreateBuilder(SqlDataReaderreader)
{
ReflectionBuilder
<
T
>
result
=
new
ReflectionBuilder
<
T
>
();
result.properties
=
new
PropertyInfo[reader.FieldCount];
for
(
int
i
=
0
;i
<
reader.FieldCount;i
++
)
{
result.properties[i]
=
typeof
(T).GetProperty(reader.GetName(i));
}

return
result;
}
}
}
using System; using System.Data.SqlClient; using System.Reflection; namespace DynamicMappingSpike { public class ReflectionBuilder<T> { private PropertyInfo[] properties; private ReflectionBuilder() { } public T Build(SqlDataReader reader) { T result = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < reader.FieldCount; i++) { if (properties[i] != null && !reader.IsDBNull(i)) { properties[i].SetValue(result, reader[i], null); } } return result; } public static ReflectionBuilder<T> CreateBuilder(SqlDataReader reader) { ReflectionBuilder<T> result = new ReflectionBuilder<T>(); result.properties = new PropertyInfo[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { result.properties[i] = typeof(T).GetProperty(reader.GetName(i)); } return result; } } }
第三种用法(DynaMicmethod ,基于IL)
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
using
System;
using
System.Data.SqlClient;
using
System.Reflection;
using
System.Reflection.Emit;
using
System.Data;
namespace
DownmoonTestConsole
{
public
class
DynamicBuilder
<
T
>

{
private
static
readonly
MethodInfogetValueMethod
=
typeof
(IDataRecord).GetMethod(
"
get_Item
"
,
new
Type[]
{
typeof
(
int
)}
);
private
static
readonly
MethodInfoisDBNullMethod
=
typeof
(IDataRecord).GetMethod(
"
IsDBNull
"
,
new
Type[]
{
typeof
(
int
)}
);
private
delegate
TLoad(IDataRecorddataRecord);
private
Loadhandler;

private
DynamicBuilder()
{}

public
TBuild(IDataRecorddataRecord)
{
return
handler(dataRecord);
}

public
static
DynamicBuilder
<
T
>
CreateBuilder(IDataRecorddataRecord)
{
DynamicBuilder
<
T
>
dynamicBuilder
=
new
DynamicBuilder
<
T
>
();

DynamicMethodmethod
=
new
DynamicMethod(
"
DynamicCreate
"
,
typeof
(T),
new
Type[]
{
typeof
(IDataRecord)}
,
typeof
(T),
true
);
ILGeneratorgenerator
=
method.GetILGenerator();
LocalBuilderresult
=
generator.DeclareLocal(
typeof
(T));
generator.Emit(OpCodes.Newobj,
typeof
(T).GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Stloc,result);
for
(
int
i
=
0
;i
<
dataRecord.FieldCount;i
++
)
{
PropertyInfopropertyInfo
=
typeof
(T).GetProperty(dataRecord.GetName(i));
LabelendIfLabel
=
generator.DefineLabel();
if
(propertyInfo
!=
null
&&
propertyInfo.GetSetMethod()
!=
null
)
{
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4,i);
generator.Emit(OpCodes.Callvirt,isDBNullMethod);
generator.Emit(OpCodes.Brtrue,endIfLabel);
generator.Emit(OpCodes.Ldloc,result);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4,i);
generator.Emit(OpCodes.Callvirt,getValueMethod);
generator.Emit(OpCodes.Unbox_Any,dataRecord.GetFieldType(i));
generator.Emit(OpCodes.Callvirt,propertyInfo.GetSetMethod());
generator.MarkLabel(endIfLabel);
}
}

generator.Emit(OpCodes.Ldloc,result);
generator.Emit(OpCodes.Ret);
dynamicBuilder.handler
=
(Load)method.CreateDelegate(
typeof
(Load));
return
dynamicBuilder;
}
}
}
using System; using System.Data.SqlClient; using System.Reflection; using System.Reflection.Emit; using System.Data; namespace DownmoonTestConsole { public class DynamicBuilder<T> { private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item", new Type[] { typeof(int) }); private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull", new Type[] { typeof(int) }); private delegate T Load(IDataRecord dataRecord); private Load handler; private DynamicBuilder() { } public T Build(IDataRecord dataRecord) { return handler(dataRecord); } public static DynamicBuilder<T> CreateBuilder(IDataRecord dataRecord) { DynamicBuilder<T> dynamicBuilder = new DynamicBuilder<T>(); DynamicMethod method = new DynamicMethod("DynamicCreate", typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true); ILGenerator generator = method.GetILGenerator(); LocalBuilder result = generator.DeclareLocal(typeof(T)); generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < dataRecord.FieldCount; i++) { PropertyInfo propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i)); Label endIfLabel = generator.DefineLabel(); if (propertyInfo != null && propertyInfo.GetSetMethod() != null) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod); generator.Emit(OpCodes.Brtrue, endIfLabel); generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, getValueMethod); generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i)); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); generator.MarkLabel(endIfLabel); } } generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret); dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return dynamicBuilder; } } }
基于的实体类
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->

using
System;
namespace
Agronet.Product.Framework.Components
{
Product
#region
Product
public
class
Product
{
private
long
m_P_ID;
/**/
///
<summary>
///
产品ID
///
</summary>
public
long
P_ID
{
get
{
return
m_P_ID;}

set
{m_P_ID
=
value;}
}

private
string
m_P_Name;
/**/
///
<summary>
///
产品名称
///
</summary>
public
string
P_Name
{
get
{
return
m_P_Name;}

set
{m_P_Name
=
value;}
}

private
int
m_P_E_ID;
/**/
///
<summary>
///
分类扩展属性ID
///
</summary>
public
int
P_E_ID
{
get
{
return
m_P_E_ID;}

set
{m_P_E_ID
=
value;}
}

private
int
m_CategoryID1;
/**/
///
<summary>
///
产品产业分类
///
</summary>
public
int
CategoryID1
{
get
{
return
m_CategoryID1;}

set
{m_CategoryID1
=
value;}
}

private
int
m_CategoryID2;
/**/
///
<summary>
///
产品行业分类
///
</summary>
public
int
CategoryID2
{
get
{
return
m_CategoryID2;}

set
{m_CategoryID2
=
value;}
}

private
int
m_CategoryID3;
/**/
///
<summary>
///
第三分类ID
///
</summary>
public
int
CategoryID3
{
get
{
return
m_CategoryID3;}

set
{m_CategoryID3
=
value;}
}

private
int
m_CategoryID4;
/**/
///
<summary>
///
第四分类ID
///
</summary>
public
int
CategoryID4
{
get
{
return
m_CategoryID4;}

set
{m_CategoryID4
=
value;}
}

private
int
m_CategoryID5;
/**/
///
<summary>
///
第五分类ID
///
</summary>
public
int
CategoryID5
{
get
{
return
m_CategoryID5;}

set
{m_CategoryID5
=
value;}
}

private
int
m_CategoryID6;
/**/
///
<summary>
///
第六分类ID
///
</summary>
public
int
CategoryID6
{
get
{
return
m_CategoryID6;}

set
{m_CategoryID6
=
value;}
}

private
string
m_P_SysCate;
/**/
///
<summary>
///
系统分类(旧)
///
</summary>
public
string
P_SysCate
{
get
{
return
m_P_SysCate;}

set
{m_P_SysCate
=
value;}
}

private
string
m_P_Key;
/**/
///
<summary>
///
关键字
///
</summary>
public
string
P_Key
{
get
{
return
m_P_Key;}

set
{m_P_Key
=
value;}
}

private
string
m_P_SellArea;
/**/
///
<summary>
///
产品销售地区
///
</summary>
public
string
P_SellArea
{
get
{
return
m_P_SellArea;}

set
{m_P_SellArea
=
value;}
}

private
string
m_P_SingleIntro;
/**/
///
<summary>
///
简介
///
</summary>
public
string
P_SingleIntro
{
get
{
return
m_P_SingleIntro;}

set
{m_P_SingleIntro
=
value;}
}

private
string
m_P_Intro;
/**/
///
<summary>
///
介绍
///
</summary>
public
string
P_Intro
{
get
{
return
m_P_Intro;}

set
{m_P_Intro
=
value;}
}

private
double
m_P_Order;
/**/
///
<summary>
///
排序
///
</summary>
public
double
P_Order
{
get
{
return
m_P_Order;}

set
{m_P_Order
=
value;}
}

private
string
m_P_Cate;
/**/
///
<summary>
///
分类(旧)
///
</summary>
public
string
P_Cate
{
get
{
return
m_P_Cate;}

set
{m_P_Cate
=
value;}
}

private
int
m_P_Cate1;
/**/
///
<summary>
///
产品原分类1
///
</summary>
public
int
P_Cate1
{
get
{
return
m_P_Cate1;}

set
{m_P_Cate1
=
value;}
}

private
int
m_P_Cate2;
/**/
///
<summary>
///
产品原分类2
///
</summary>
public
int
P_Cate2
{
get
{
return
m_P_Cate2;}

set
{m_P_Cate2
=
value;}
}

private
int
m_P_Cate3;
/**/
///
<summary>
///
产品原分类3
///
</summary>
public
int
P_Cate3
{
get
{
return
m_P_Cate3;}

set
{m_P_Cate3
=
value;}
}

private
int
m_P_C_Order;
/**/
///
<summary>
///
企业顺序
///
</summary>
public
int
P_C_Order
{
get
{
return
m_P_C_Order;}

set
{m_P_C_Order
=
value;}
}

private
DateTimem_P_TopTime;
/**/
///
<summary>
///
置顶时间
///
</summary>
public
DateTimeP_TopTime
{
get
{
return
m_P_TopTime;}

set
{m_P_TopTime
=
value;}
}

private
DateTimem_AddTime;
/**/
///
<summary>
///
新增时间
///
</summary>
public
DateTimeAddTime
{
get
{
return
m_AddTime;}

set
{m_AddTime
=
value;}
}

private
string
m_AddUser;
/**/
///
<summary>
///
新增用户
///
</summary>
public
string
AddUser
{
get
{
return
m_AddUser;}

set
{m_AddUser
=
value;}
}

private
DateTimem_ModTime;
/**/
///
<summary>
///
修改时间
///
</summary>
public
DateTimeModTime
{
get
{
return
m_ModTime;}

set
{m_ModTime
=
value;}
}

private
string
m_ModUser;
/**/
///
<summary>
///
修改人
///
</summary>
public
string
ModUser
{
get
{
return
m_ModUser;}

set
{m_ModUser
=
value;}
}

private
string
m_P_BigImage;
/**/
///
<summary>
///
大图地址
///
</summary>
public
string
P_BigImage
{
get
{
return
m_P_BigImage;}

set
{m_P_BigImage
=
value;}
}

private
string
m_P_SmallImage;
/**/
///
<summary>
///
小图地址
///
</summary>
public
string
P_SmallImage
{
get
{
return
m_P_SmallImage;}

set
{m_P_SmallImage
=
value;}
}

private
string
m_P_BigImage2;
/**/
///
<summary>
///
大图地址2
///
</summary>
public
string
P_BigImage2
{
get
{
return
m_P_BigImage2;}

set
{m_P_BigImage2
=
value;}
}

private
string
m_P_SmallImage2;
/**/
///
<summary>
///
小图地址2
///
</summary>
public
string
P_SmallImage2
{
get
{
return
m_P_SmallImage2;}

set
{m_P_SmallImage2
=
value;}
}

private
string
m_P_BigImage3;
/**/
///
<summary>
///
大图地址3
///
</summary>
public
string
P_BigImage3
{
get
{
return
m_P_BigImage3;}

set
{m_P_BigImage3
=
value;}
}

private
string
m_P_SmallImage3;
/**/
///
<summary>
///
小图地址3
///
</summary>
public
string
P_SmallImage3
{
get
{
return
m_P_SmallImage3;}

set
{m_P_SmallImage3
=
value;}
}

private
string
m_LoginID;
/**/
///
<summary>
///
登录帐号
///
</summary>
public
string
LoginID
{
get
{
return
m_LoginID;}

set
{m_LoginID
=
value;}
}

private
short
m_CurState;
/**/
///
<summary>
///
审核状态
///
</summary>
public
short
CurState
{
get
{
return
m_CurState;}

set
{m_CurState
=
value;}
}

private
short
m_RecState;
/**/
///
<summary>
///
推荐状态
///
</summary>
public
short
RecState
{
get
{
return
m_RecState;}

set
{m_RecState
=
value;}
}
private
string
m_P_CheckInfo;
/**/
///
<summary>
///
审核信息
///
</summary>
public
string
P_CheckInfo
{
get
{
return
m_P_CheckInfo;}

set
{m_P_CheckInfo
=
value;}
}

private
string
m_P_Max;
/**/
///
<summary>
///
单价上限
///
</summary>
public
string
P_Max
{
get
{
return
m_P_Max;}

set
{m_P_Max
=
value;}
}

private
string
m_P_Min;
/**/
///
<summary>
///
单价下限
///
</summary>
public
string
P_Min
{
get
{
return
m_P_Min;}

set
{m_P_Min
=
value;}
}

private
string
m_P_Unit;
/**/
///
<summary>
///
单价单位
///
</summary>
public
string
P_Unit
{
get
{
return
m_P_Unit;}

set
{m_P_Unit
=
value;}
}

private
int
m_P_L_ID;
/**/
///
<summary>
///
商标ID
///
</summary>
public
int
P_L_ID
{
get
{
return
m_P_L_ID;}

set
{m_P_L_ID
=
value;}
}

private
int
m_C_ID;
/**/
///
<summary>
///
企业ID
///
</summary>
public
int
C_ID
{
get
{
return
m_C_ID;}

set
{m_C_ID
=
value;}
}

private
int
m_C_I_ID;
/**/
///
<summary>
///
联系方式ID
///
</summary>
public
int
C_I_ID
{
get
{
return
m_C_I_ID;}

set
{m_C_I_ID
=
value;}
}

private
string
m_P_NewInfo;
/**/
///
<summary>
///
扩展联系方式
///
</summary>
public
string
P_NewInfo
{
get
{
return
m_P_NewInfo;}

set
{m_P_NewInfo
=
value;}
}

private
string
m_P_Price;
/**/
///
<summary>
///
价格(旧)
///
</summary>
public
string
P_Price
{
get
{
return
m_P_Price;}

set
{m_P_Price
=
value;}
}

private
int
m_P_OtherState;
/**/
///
<summary>
///
其他状态
///
</summary>
public
int
P_OtherState
{
get
{
return
m_P_OtherState;}

set
{m_P_OtherState
=
value;}
}

private
string
m_P_NewKey1;
/**/
///
<summary>
///
新关键字一
///
</summary>
public
string
P_NewKey1
{
get
{
return
m_P_NewKey1;}

set
{m_P_NewKey1
=
value;}
}

private
string
m_P_NewKey2;
/**/
///
<summary>
///
新关键字二
///
</summary>
public
string
P_NewKey2
{
get
{
return
m_P_NewKey2;}

set
{m_P_NewKey2
=
value;}
}

private
string
m_P_NewKey3;
/**/
///
<summary>
///
新关键字三
///
</summary>
public
string
P_NewKey3
{
get
{
return
m_P_NewKey3;}

set
{m_P_NewKey3
=
value;}
}

private
int
m_F1;
/**/
///
<summary>
///
扩展1
///
</summary>
public
int
F1
{
get
{
return
m_F1;}

set
{m_F1
=
value;}
}

private
int
m_F2;
/**/
///
<summary>
///
扩展2
///
</summary>
public
int
F2
{
get
{
return
m_F2;}

set
{m_F2
=
value;}
}

private
string
m_F3;
/**/
///
<summary>
///
扩展3
///
</summary>
public
string
F3
{
get
{
return
m_F3;}

set
{m_F3
=
value;}
}

private
string
m_F4;
/**/
///
<summary>
///
扩展4
///
</summary>
public
string
F4
{
get
{
return
m_F4;}

set
{m_F4
=
value;}
}

private
string
m_WebSite;
/**/
///
<summary>
///
网站
///
</summary>
public
string
WebSite
{
get
{
return
m_WebSite;}

set
{m_WebSite
=
value;}
}

private
string
m_Languages;
/**/
///
<summary>
///
语言
///
</summary>
public
string
Languages
{
get
{
return
m_Languages;}

set
{m_Languages
=
value;}
}

}
#endregion


}
测试主要方法
static SqlConnection connection = new SqlConnection(@"server=localhost;database=Testbigdata;uid=sa;pwd=sa"); static SqlCommand command = new SqlCommand("Select top 100 * From product order by P_ID asc ", connection); public static void LoadManually() { using (SqlConnection connection = new SqlConnection(ConnStr)) { SqlCommand command = new SqlCommand(CmdStr, connection); connection.Open(); LoadManually(command); connection.Close(); } } public static void LoadUsingReflection() { using (SqlConnection connection = new SqlConnection(ConnStr)) { SqlCommand command = new SqlCommand(CmdStr, connection); connection.Open(); LoadUsingReflection(command); connection.Close(); } } public static void LoadUsingDynamicCode() { using (SqlConnection connection = new SqlConnection(ConnStr)) { SqlCommand command = new SqlCommand(CmdStr, connection); connection.Open(); LoadUsingDynamicCode(command); connection.Close(); } } private static void LoadManually(SqlCommand command) { DateTime start = DateTime.Now; Console.WriteLine("Start Manual Load: {0}", start); using (SqlDataReader reader = command.ExecuteReader()) { ManualBuilder builder = new ManualBuilder(); while (reader.Read()) { Agronet.Product.Framework.Components.Product m_Product = builder.Build(reader); } } DateTime stop = DateTime.Now; Console.WriteLine("Stop Manual Load: {0}", stop); Console.WriteLine("Elapsed Time: {0}", stop - start); Console.WriteLine(); } private static void LoadUsingReflection(SqlCommand command) { DateTime start = DateTime.Now; Console.WriteLine("Start Reflection Load: {0}", start); using (SqlDataReader reader = command.ExecuteReader()) { ReflectionBuilder<Agronet.Product.Framework.Components.Product> builder = ReflectionBuilder<Agronet.Product.Framework.Components.Product>.CreateBuilder(reader); while (reader.Read()) { Agronet.Product.Framework.Components.Product m_Product = builder.Build(reader); } } DateTime stop = DateTime.Now; Console.WriteLine("Stop Reflection Load: {0}", stop); Console.WriteLine("Elapsed Time: {0}", stop - start); Console.WriteLine(); } private static void LoadUsingDynamicCode(SqlCommand command) { DateTime start = DateTime.Now; Console.WriteLine("Start Dynamic Load: {0}", start); using (SqlDataReader reader = command.ExecuteReader()) { DynamicBuilder<Agronet.Product.Framework.Components.Product> builder = DynamicBuilder<Agronet.Product.Framework.Components.Product>.CreateBuilder(reader); while (reader.Read()) { Agronet.Product.Framework.Components.Product m_Product = builder.Build(reader); //Console.WriteLine("ID:"+m_Product.P_ID); //Console.WriteLine("Name:"+m_Product.P_Name); //Console.WriteLine("LoginID:"+m_Product.LoginID); } } DateTime stop = DateTime.Now; Console.WriteLine("Stop Dynamic Load: {0}", stop); Console.WriteLine("Elapsed Time: {0}", stop - start); Console.WriteLine(); }
在网络流量200的负载测试下
结果如图



结论 在vs2005环境下,从Reader流转化为List<T>或IList<T>时,Reflect方法效率最低,DynamicMethod次之,但性能与Manual方法相当接近,综合而言, DynamicMethod 代码量小,移植性好,性能相对也不错,值得推荐!
最主要的是:当Entity的属性值个数大于Reader的Column个数时,Reflect和DynamicMethod性能更佳,且不会报错!换言之,当你需取表中的某几个字段时,DynamicMwthod的优势是显而易见的!

被折叠的 条评论
为什么被折叠?



