首页 > 开发 > .Net > 正文

ADO.NET对象的构造(3)_DataTable(续)

2020-02-03 15:56:23
字体:
来源:转载
供稿:网友

n overloads public overridable function add() as datatable

n overloads public overridable sub add(byval table as datatable)

n overloads public overridable function add(byval name as string) as datatable

n public sub addrange(byval tables() as datatable)

参数

1. table 要添加的 datatable 对象。

2. name 要赋予已创建的 datatable 的名称。

3. tables 要添加到集合中的 datatable 对象的数组。



datatablecollection 包含特定 dataset 的所有 datatable 对象。若要访问 dataset 的 datatablecollection,请使用 tables 属性。

datatablecollection 使用诸如 add、clear 和 remove 之类的方法管理集合中的项目。

使用 contains 方法确定集合中是否有特定表(由索引或名称指定)。

若要从一个表浏览到另一个表,请使用 datatable 的 childrelations 或 parentrelations 属性来访问它的 datarelation 对象的集合。还可使用 relations 属性浏览给定的 dataset 集合中 datatables 的父/子关系。



示例

private sub addtable()

dim dset as dataset= ctype(datagrid1.datasource, dataset)

dim dt as datatable



dt = dset.tables.add("mynewtable")

messagebox.show(dt.tablename)

messagebox.show(dset.tables.count.tostring() & " tables")



dim i as integer

for i = 0 to 2

dset.tables.add()

next i

messagebox.show(dset.tables.count.tostring() & " tables")



dim tables as datatablecollection = ctype(datagrid1.datasource, dataset).tables

tables.add(new datatable)

messagebox.show(dset.tables.count.tostring() & " tables")



dim t as datatable

dim r as datarow

dim c as datacolumn

for each t in dset.tables

console.writeline(t.tablename)

for each r in t.rows

for each c in t.columns

if not (r(c) is nothing) then

console.writeline(r(c))

end if

next

next

next



dim t1 as datatable = new datatable("customers" )

t1.columns.add("customerid", type.gettype("system.int32")).autoincrement = true

t1.columns.add("name", type.gettype("system.string"))

t1.primarykey = new datacolumn() { t1.columns("customerid") }



dim t2 as datatable = new datatable("orders" )

t2.columns.add("orderid", type.gettype("system.int32")).autoincrement = true

t2.columns.add("customerid", type.gettype("system.int32"))

t2.columns.add("amount", type.gettype("system.double"))

t2.primarykey = new datacolumn() { t2.columns("orderid") }



dset.tables.addrange( new datatable() {t1, t2} )



for each t in dset.tables

console.writeline(t.tablename )

for each c in t.columns

console.write("{0}" & vbtab, c.columnname)

next

console.writeline()

next

messagebox.show(dset.tables.count.tostring() & " tables")

end sub

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表