会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
漫漫人生路···
博客园
首页
新随笔
新文章
联系
订阅
管理
.net中给数据表实体类赋值的方法
1、首先做个实体类
Entity.cs
Code
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
Entity
{
private
string
_a;
private
string
_b;
private
string
_c;
public
string
a
{
get
{
return
_a; }
set
{ _a
=
value; }
}
public
string
b
{
get
{
return
_b; }
set
{ _b
=
value; }
}
public
string
c
{
get
{
return
_c; }
set
{ _c
=
value; }
}
}
}
2、进行赋值
Program
.cs
Code
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Reflection;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
DataSet ds
=
new
DataSet();
DataTable dt
=
new
DataTable();
dt.Columns.Add(
"
a
"
,
typeof
(
string
));
dt.Columns.Add(
"
b
"
,
typeof
(
string
));
dt.Columns.Add(
"
c
"
,
typeof
(
string
));
DataRow dr
=
dt.NewRow();
dr[
"
a
"
]
=
"
aa
"
;
dr[
"
b
"
]
=
"
bb
"
;
dr[
"
c
"
]
=
"
cc
"
;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
Entity entity
=
new
Entity();
foreach
(DataRow row1
in
ds.Tables[
0
].Rows)
{
foreach
(DataColumn col
in
row1.Table.Columns)
{
PropertyInfo pi
=
typeof
(Entity).GetProperty(col.ColumnName);
pi.SetValue(entity, ds.Tables[
0
].Rows[0][col.ColumnName].ToString(),
null
);
}
}
System.Console.WriteLine(entity.a);
System.Console.WriteLine(entity.b);
System.Console.WriteLine(entity.c);
}
}
}
posted on
2009-10-20 14:45
Liran
阅读(
425
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部