2013-03-14 70 views
0

在我的代碼中,我有一個函數,它從數據庫中返回一行數據。它使用Structure返回數據,因此我可以返回多個數據。一個函數能返回多於一行的數據嗎?

例如:

Public Structure structure_name 
    Public column1 as integer 
    Public column2 as string 
    Public column3 as string 
End Structure 

Function function_name() As structure_name 
    Dim single_row_structure as structure_name 

    'get single row from database 
    'assign single row values to attributes of Structure 

    single_row_strucure.column1 = dbcol1 
    single_row_strucure.column2 = dbcol2 
    single_row_strucure.column3 = dbcol3 

    return single_row_strucure 
End Function 

這工作正常單行。我的問題是,我如何獲得一個返回多於一行數據的函數?

+0

爲什麼你可以不返回行的集合? – Alex 2013-03-14 15:45:54

+0

'Function function_name()as structure_name()'應該工作嗎? – jgauffin 2013-03-14 15:50:01

回答

1
Function function_name() As structure_name() 

    Dim rows(2) as structure_name 
    rows(0).column1 = dbcol1 
    rows(0).column2 = dbcol2 
    rows(0).column3 = dbcol3 
    rows(1).column1 = dbcol1 
    rows(1).column2 = dbcol2 
    rows(1).column3 = dbcol3 

    return rows 
End Function 

http://www.vb-helper.com/howto_net_declare_arrays.html

+0

我知道如何改變這個,所以行數是基於從數據庫返回的行數,但是如何在function_name()之外使用返回的結構數組呢?如果行數由數據庫返回的行數決定? – oshirowanen 2013-03-15 12:24:53

0

是的,你可以返回某種類型的集合。因爲您的標籤建議您使用.NET的第一個版本,所以在語法選項中可能會有一些限制。 (1.1 - 是真的嗎?)

使用最新版本的.NET和C#,我會返回List<structure_name>。但是還有很多其他的選擇。

就從數據庫中獲取數據而言,數據庫非常適合返回多行數據。但是您需要提供有關哪個數據庫以及您如何獲取數據的更多詳細信息,以便提供有關如何將其傳輸到C#集合的建議。

0

對不起一段時間,因爲VB所以C#

爲什麼不使用集就像一個列表

List<structure_name> structure_names = new List<structure_name>(); 

structure_names.Add(single_row_strucure); // repeat