ADO.NET對象的構造(3)_DataTable(續)
2024-07-10 12:59:38
供稿:網友
 
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