zoukankan
html css js c++ java
DataTable应用
code
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public
partial
class
DataTable应用 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
btnCreate_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
new
DataSet(
"
myDS
"
);
//
创建数据集
DataTable dtMaster
=
new
DataTable(
"
Master
"
);
//
创建数据表
DataTable dtChild
=
new
DataTable(
"
Child
"
);
dsUntyped.Tables.Add(dtMaster);
//
把数据表添加到数据集中
dsUntyped.Tables.Add(dtChild);
Session[
"
ds
"
]
=
dsUntyped;
}
protected
void
btnAddColumn_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dsUntyped.Tables[
"
Master
"
].Columns.Add(
"
MasterID
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Master
"
].Columns.Add(
"
MasterValue
"
,
typeof
(
string
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
MasterLink
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
ChildID
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
ChildValue
"
,
typeof
(
string
));
//
修改表头
dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
].Caption
=
"
主ID
"
;
dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterValue
"
].Caption
=
"
值
"
;
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
protected
void
btnAddRow_Click(
object
sender, System.EventArgs e)
{
try
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
//
为Master表添加两行
DataRow dr
=
dsUntyped.Tables[
"
Master
"
].NewRow();
dr[
"
MasterID
"
]
=
1
;
dr[
"
MasterValue
"
]
=
"
One
"
;
dsUntyped.Tables[
"
Master
"
].Rows.Add(dr);
dr
=
dsUntyped.Tables[
"
Master
"
].NewRow();
dr[
"
MasterID
"
]
=
2
;
dr[
"
MasterValue
"
]
=
"
Two
"
;
dsUntyped.Tables[
"
Master
"
].Rows.Add(dr);
//
为child表添加1行
dr
=
dsUntyped.Tables[
"
Child
"
].NewRow();
dr[
"
MasterLink
"
]
=
1
;
dr[
"
ChildID
"
]
=
1
;
dr[
"
ChildValue
"
]
=
"
ChildOne
"
;
dsUntyped.Tables[
"
Child
"
].Rows.Add(dr);
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
catch
(Exception ee)
{
Response.Write(ee.Message);
}
}
//
添加唯一键
protected
void
Button1_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
//
表示对一组列的限制,列中所有值必须是唯一的
System.Data.UniqueConstraint uc
=
new
UniqueConstraint(
"
unqi
"
, dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
]);
dsUntyped.Tables[
"
Master
"
].Constraints.Add(uc);
Session[
"
ds
"
]
=
dsUntyped;
}
private
void
Bind()
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dgMaster.DataSource
=
dsUntyped.Tables[
"
Master
"
].DefaultView;
dgChild.DataSource
=
dsUntyped.Tables[
"
Child
"
].DefaultView;
this
.DataBind();
}
protected
void
btnAddForeign_Click(
object
sender, System.EventArgs e)
{
//
添加外键
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
System.Data.ForeignKeyConstraint fc
=
new
ForeignKeyConstraint(
"
fc
"
, dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
], dsUntyped.Tables[
"
Child
"
].Columns[
"
MasterLink
"
]);
dsUntyped.Tables[
"
Child
"
].Constraints.Add(fc);
Session[
"
ds
"
]
=
dsUntyped;
}
protected
void
btnUpdateMID_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dsUntyped.Tables[
"
Master
"
].Rows[
0
][
"
MasterID
"
]
=
4
;
Bind();
}
protected
void
Button2_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
int
nIndexTb
=
int
.Parse(ddlTable.SelectedItem.Value);
int
nIndexRow
=
int
.Parse(tbRow.Text);
int
nIndexCol
=
int
.Parse(tbCol.Text);
object
obj
=
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol];
tbResult.Text
=
obj.ToString();
}
protected
void
btnUpdateDs_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
int
nIndexTb
=
int
.Parse(ddlTable.SelectedItem.Value);
int
nIndexRow
=
int
.Parse(tbRow.Text);
int
nIndexCol
=
int
.Parse(tbCol.Text);
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol]
=
tbResult.Text;
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
}
查看全文
相关阅读:
差分约束系统详解
AC自动机详解
KMP算法详解
ST算法详解
Trie详解
欧拉路径详解
树上差分详解
LCA详解
树链剖分详解
树的直径详解
原文地址:https://www.cnblogs.com/hubcarl/p/1423536.html
最新文章
Java中request请求之
页面小功能
细话
数据库连接池配置
web.xml配置详情
CF_321_B_NetFlow
插值计算的简单理解
G_M_C_美食节
CF_400_D
CF_884_F(NetFlow)
热门文章
CF_863_F(Netflow)
G_M_网络流A_网络吞吐量
点分治经典_动态点分治
SDOI2011_染色
AVL模板
Pycharm添加python2解释器
如何写对二分查找的代码而不出错
机器学习面试
L1不可导的时候该怎么办
使用Python进行层次聚类
Copyright © 2011-2022 走看看