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();
}
}
查看全文
相关阅读:
spring事物管理五种配置方式
解决html中内部元素mouse事件干扰
普通类获取ApplicationContext(附带servletContext)的方法
SQL学习基础知识
vs 工具技巧手册
wcf client与webservice通信备注
BackgroundWorker学习一
silverlight发布注意事项
将wcf 以webservice的方式调用
Visual Studio 2005 Team Foundation Server (TFS)单服务器安装记
原文地址:https://www.cnblogs.com/hubcarl/p/1423536.html
最新文章
C#中的yield关键字的一个用法
同步与异步区别,Invoke与BeginInoke的区别与一点理解
WPF用ListBox做简单的柱状图
再谈ObservableCollection
又一次Linq查询优化
C#中的迭代器基础
C#中的where泛型约束中的new()构造函数约束
Asp.net中的一个判断session是否合法的做法
centos 离线安装K8S
零散记录
热门文章
K8S内存泄漏问题处理
截获客户端屏幕信息小程式
SQL 杂文笔记
隐藏进程VB类库
兼职,有需要的找我
int+? int后带问题是什么意思
安家了.
IDEA 类注释、方法注释模板配置
IDEA 开发常用配置
IDEA 常用快捷键总结
Copyright © 2011-2022 走看看