简体   繁体   中英

Create a function to get dataset in vb.net

I have made dataset as a datatable, now I need to get 3 more data tables to show on my graph, however to do it all 3 times, I was told I can make a function and call it from the original datatable. But how would I call the existing datatable?

My code was:

    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

Using this, how can I make a new function so I don't need to copy and paste a new dataset, as I just want to make a new function and call my datatable from the function, so my code is neater.

So far I have

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

It's wrong somewhere...

I think something like this is what you are after. It enumerates through all DataTables in the DataSet and all rows in each DataTable and adds the information in the data column to the 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

Private Function 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM