简体   繁体   English

创建一个函数以在vb.net中获取数据集

[英]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. 我已经将数据集作为一个数据表,现在我需要再获得3个数据表才能显示在图形上,但是要全部执行3次,我被告知可以创建一个函数并从原始数据表中调用它。 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: 它通过枚举所有DataTables中的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

Private Function GetDataTable(ByVal cmd As SqlCommand) As DataTable 私有函数GetDataTable(ByVal cmd作为SqlCommand)作为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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