robot.h--为之漫笔
博客园
首页
博问
闪存
订阅
管理
2008年5月4日
DemonstrateMergeDataTable
From:MSDN Example
private
static
void
DemonstrateMergeTable()
{
//
Demonstrate merging, within and without
//
preserving changes.
//
In this example, take these actions:
//
1. Create a DataTable (table1) and fill the table with data.
//
2. Create a copy of table1, and modify its data (modifiedTable).
//
3. Modify data in table1.
//
4. Make a copy of table1 (table1Copy).
//
5. Merge the data from modifiedTable into table1 and table1Copy,
//
showing the difference between setting the preserveChanges
//
parameter to true and false.
//
Create a new DataTable.
DataTable table1
=
new
DataTable(
"
Items
"
);
//
Add two columns to the table:
DataColumn column
=
new
DataColumn(
"
id
"
,
typeof
(System.Int32));
column.AutoIncrement
=
true
;
table1.Columns.Add(column);
column
=
new
DataColumn(
"
item
"
,
typeof
(System.String));
table1.Columns.Add(column);
//
Set primary key column.
table1.PrimaryKey
=
new
DataColumn[]
{ table1.Columns[
0
] }
;
//
Add some rows.
DataRow row;
for
(
int
i
=
0
; i
<=
3
; i
++
)
{
row
=
table1.NewRow();
row[
"
item
"
]
=
"
Item
"
+
i;
table1.Rows.Add(row);
}
//
Accept changes.
table1.AcceptChanges();
PrintValues(table1,
"
Original values
"
);
//
Using the same schema as the original table,
//
modify the data for later merge.
DataTable modifiedTable
=
table1.Copy();
foreach
(DataRow rowModified
in
modifiedTable.Rows)
{
rowModified[
"
item
"
]
=
rowModified[
"
item
"
].ToString()
+
"
modified
"
;
}
modifiedTable.AcceptChanges();
//
Change row values, and add a new row:
table1.Rows[
0
][
"
item
"
]
=
"
new Item 0
"
;
table1.Rows[
1
][
"
item
"
]
=
"
new Item 1
"
;
row
=
table1.NewRow();
row[
"
id
"
]
=
4
;
row[
"
item
"
]
=
"
Item 4
"
;
table1.Rows.Add(row);
//
Get a copy of the modified data:
DataTable table1Copy
=
table1.Copy();
PrintValues(table1,
"
Modified and new Values
"
);
PrintValues(modifiedTable,
"
Data to be merged into table1
"
);
//
Merge new data into the modified data.
table1.Merge(modifiedTable,
true
);
PrintValues(table1,
"
Merged data (preserve changes)
"
);
table1Copy.Merge(modifiedTable,
false
);
PrintValues(table1Copy,
"
Merged data (don't preserve changes)
"
);
}
private
static
void
PrintValues(DataTable table,
string
lbl)
{
//
Display the values in the supplied DataTable:
System.Web.HttpContext.Current.Response.Write(lbl
+
"
<br />
"
);
foreach
(DataRow row
in
table.Rows)
{
foreach
(DataColumn column
in
table.Columns)
{
System.Web.HttpContext.Current.Response.Write(row[column, DataRowVersion.Current]);
}
System.Web.HttpContext.Current.Response.Write(
"
<br />
"
);
}
}
posted @ 2008-05-04 20:01 roboth 阅读(23) 评论(1)
编辑
DataTable部分代码
//
Create DataTable
DataTable dt
=
new
DataTable();
dt.CaseSensitive
=
true
;
//
Create DataColumns
DataColumn col1
=
new
DataColumn(
"
id
"
,
typeof
(
int
));
DataColumn col2
=
new
DataColumn(
"
UserName
"
,
typeof
(
string
));
//
Config Propertyies
col2.AllowDBNull
=
false
;
col2.MaxLength
=
50
;
col2.Unique
=
true
;
col1.AutoIncrement
=
true
;
col1.AutoIncrementSeed
=
100
;
col1.AutoIncrementStep
=
1
;
col1.ReadOnly
=
true
;
//
the DataColumn with Expression
DataColumn col3
=
new
DataColumn(
"
Expression
"
,
typeof
(
string
));
col3.Expression
=
"
id+UserName
"
;
//
add DataColumns to dt;
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.PrimaryKey
=
new
DataColumn[]
{ dt.Columns[
0
] }
;
//
Create test data
DataRow dr
=
dt.NewRow();
dr[
1
]
=
"
Roboth
"
;
dt.Rows.Add(dr);
dr
=
dt.NewRow();
dr[
1
]
=
"
xinsoft
"
;
dt.Rows.Add(dr);
dr
=
dt.NewRow();
dr[
1
]
=
"
SpiderMan
"
;
dt.Rows.Add(dr);
//
bind to gridview
this
.gridview1.DataSource
=
dt;
this
.gridview1.DataBind();
//
example1 for edit
DataRow dr1
=
dt.Rows.Find(
102
);
if
(dr1
==
null
)
{
Response.Write(
"
no find
"
);
}
else
{
dr1.BeginEdit();
dr1[
1
]
=
"
SpiderWoman
"
;
dr1.EndEdit();
}
//
example2 for edit
object
[] obj
=
new
object
[]
{
null
,
"
Spo
"
,
null
}
;
DataRow dr2
=
dt.Rows.Find(
102
);
if
(dr2.IsNull(
2
))
//
赋值是 DBNull.Value
{
dr2[
2
]
=
"
test
"
;
}
dr2.ItemArray
=
obj;
//
example for delete
DataRow dr_D
=
dt.Rows.Find(
102
);
dr_D.Delete();
//
Remove corresponding row
dt.Rows.Remove(dt.Rows.Find(
101
));
/**/
///
/Remove corresponding row again
dt.Rows.RemoveAt(
0
);
this
.gridview2.DataSource
=
dt;
this
.gridview2.DataBind();
DataSet ds
=
new
DataSet();
Response.Write(ds.DataSetName);
posted @ 2008-05-04 18:51 roboth 阅读(65) 评论(0)
编辑
公告
昵称:
roboth
园龄:
4年10个月
粉丝:
4
关注:
3
搜索
随笔分类
.NetFramework(10)
(rss)
Ajax(2)
(rss)
Ajax
(rss)
asp.Net2.0(62)
(rss)
C#2.0(56)
(rss)
CSS+Javacript(28)
(rss)
EC(6)
(rss)
English(2)
(rss)
Interview(2)
(rss)
Office Forms Server+SharePoint Server(1)
(rss)
ProgramLife(34)
(rss)
Sqlserver(47)
(rss)
TechBase(13)
(rss)
UML(3)
(rss)
XML(5)
(rss)
宝宝成长记(2)
(rss)
每日一句英语(4)
(rss)
美食天下(2)
(rss)
随笔档案
2010年1月 (1)
2009年9月 (2)
2009年7月 (5)
2009年6月 (2)
2009年5月 (14)
2009年4月 (9)
2009年3月 (4)
2009年2月 (2)
2009年1月 (3)
2008年12月 (10)
2008年11月 (25)
2008年10月 (8)
2008年9月 (25)
2008年8月 (33)
2008年7月 (30)
2008年6月 (26)
2008年5月 (24)
2008年4月 (11)
2008年3月 (8)
2008年1月 (2)
2007年12月 (2)
2007年11月 (5)
2007年10月 (1)
2007年9月 (26)
2007年8月 (21)
2007年7月 (13)
2007年6月 (11)
2007年5月 (3)
2007年4月 (3)
积分与排名
积分 - 89658
排名 - 1148
最新评论
阅读排行榜
推荐排行榜