2013-07-01 77 views
-1

我做了數據集作爲一個數據表,現在我需要3分數據表上顯示我的圖表,但是要做到這一切的3倍,有人告訴我,我可以做一個函數並從原始數據表中調用它。但是,我將如何調用現有的數據表?創建一個函數來獲取數據集在vb.net

我的代碼是:

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 

,這是錯誤的地方......

+0

您的代碼並沒有太大的意義 - 是什麼'first''秒''third'等? –

+0

他們從表中,「信息」 commming。行是第一個第二個和第三個,以及列數據。我想要一個函數來獲取我所顯示的代碼...... –

+0

'information'是一個DataSet(根據您的代碼)不表。 –

回答

2

我覺得這樣的事情是你所追求的。它枚舉通過在DataSet所有DataTables和所有行中的每個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 
+0

怎麼來的一切說,對信息的錯誤是一個命名空間,不能用作表達式.. –

+0

@mali'information'是張貼在你的代碼 –

+0

確定你的數據集,我有,現在我該怎樣把這種負載..因爲我的一切僅僅是一個函數它並沒有叫任何地方 –

1

專用功能GetDataTable(BYVAL CMD作爲的SqlCommand)作爲數據表

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 
相關問題