-1

我已经将数据集作为数据表,现在我需要在我的图表上显示另外 3 个数据表,但是要全部完成 3 次,我被告知我可以创建一个函数并从原始数据表中调用它。但是我将如何调用现有的数据表?

我的代码是:

    Dim a As DataSet = information
    Dim abc As DataTable
    abc = a.Tables(0)

    Dim array As New ArrayList
    Dim array1 As New ArrayList

    For Each row In first
        array.Add(row("data"))
    Next row

    For Each row In second
        array1.Add(row("data"))
    Next row

    For Each row In third
        array2.Add(row("data"))
    Dim serializer As New JavaScriptSerializer()
    Dim arrayJson As String = serializer.Serialize(array)
End Sub

使用它,我怎样才能创建一个新函数,这样我就不需要复制和粘贴一个新的数据集,因为我只想创建一个新函数并从函数中调用我的数据表,所以我的代码更整洁。

到目前为止我有

Function information() As DataTable
    Dim array As New ArrayList
    For Each row In forth
        array.Add(row("data"))
    Next row
End Function

哪里不对。。。

4

2 回答 2

2

我认为这样的事情就是你所追求的。它枚举了 allDataTables中的DataSet所有行和每行中的所有行,DataTable并将数据列中的信息添加到 arraylist:

Dim ds As DataSet = information 'populate the DataSet with data
Dim arr(ds.Tables.Count - 1) As New ArrayList 'define an Array of ArrayLists to hold the data 

'Loop through each Table and put the data into the appropriate ArrayList
For i As Integer = 0 To ds.Tables.Count - 1
    For Each dr As DataRow in ds.Tables(i).Rows
        arr(i).Add(dr("data"))
    Next
    'Do whatever you want with the arr here...
Next
于 2013-07-01T15:40:22.983 回答
1

私有函数 GetDataTable(ByVal cmd As SqlCommand) As DataTable

    Dim dt As New DataTable()
    Dim strConnString As [String] = System.Configuration _
    .ConfigurationManager.ConnectionStrings("TransferConnectionString").ConnectionString

    Dim con As New SqlConnection(strConnString)
    Dim sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    Try
        con.Open()
        sda.SelectCommand = cmd
        sda.Fill(dt)
        Return dt
    Catch ex As Exception
        Throw ex
    Finally
        con.Close()
        sda.Dispose()
        con.Dispose()
    End Try
End Function
于 2014-09-10T08:35:17.450 回答