2013-02-23 100 views
2

我需要创建几个(我不知道有多少个直到运行时)表并将每个表添加到数据集。每个数据表都需要有一个不同的名称,可以根据循环变量构建。创建(在循环中)包含多个表的数据集

sub fillDataTableA(byref dt as datatable) 
... code to fill table 
end sub 
sub fillDataTableB(byref dt as datatable) 
... code to fill table 
end sub 

loop through branches 
    dim dt (name it as "branch1 A")  ' "branch#" comes from loop 
    fillDataTableA(dt) 
    dataset.add (dt)   ' add this table to dataset 

    dim dt (name it as "branch1 B") 
    fillDataTableB(dt)  
    dataset.add (dt) ' add second table for this branch 
next (branch) 

processDataSets(dataset) 

所以,如果有4个分支,将会有8个表添加到数据集。 我的问题是我不知道如何将它们(表格)以不同的方式命名并动态添加到数据集中。

你能看到如何解决这个问题吗? 谢谢!

回答

0

这是你所需要的?

Dim ds As New DataSet 

for i as integer=0 to 9 

Dim dt As New DataTable("NameOfTable" & i.tostring) 
ds.Tables.Add(dt) 

Next 
+0

也许。循环在哪里适合这个?这很重要,因为每个新表都必须更改“NameOfTable”。 – user2103442 2013-02-24 00:24:03

+0

编辑包括循环...非常类似于PeterG – Sparers 2013-02-24 22:10:27

2

这足以让你开始?

Dim n As Integer 
    Dim ds As New DataSet 

    Dim s = InputBox("Number of tables?") 
    n = Integer.Parse(s) 

    For i = 1 To n 
     Dim t As New DataTable With {.TableName = "newtable" & i.ToString} 
     ds.Tables.Add(t) 
    Next 
+0

+1我不知道你可以用{}内联,这是一个巧妙的技巧! – Jesse 2013-02-24 01:32:37