vb.net 如何将数据库查询结果添加到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14293023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add database query results to an array
提问by ken
I am trying to select string values from an Access database and then place them into an array of strings so that I can perform a loop statement on the array.
我试图从 Access 数据库中选择字符串值,然后将它们放入一个字符串数组中,以便我可以对数组执行循环语句。
However I don't know how to place the result of the query into an array. I know how to query the database but all I need is how to put the result in an array.
但是我不知道如何将查询结果放入数组中。我知道如何查询数据库,但我所需要的只是如何将结果放入数组中。
My select statement is Select motonum from moto
. I want to put motonum
in an array.
我的选择语句是Select motonum from moto
. 我想放入motonum
一个数组。
The whole code to read the data is:
读取数据的整个代码是:
connect2()
If Not cnn2.State = ConnectionState.Open Then
'open connection
cnn2.Open()
'MessageBox.Show("chk2")
End If
cmd5.Connection = cnn2
cmd5.CommandText = "Select motonum from moto"
myData5 = cmd5.ExecuteReader
While myData5.Read
'code to return results here
End While`
回答by XIVSolutions
There are any number of different ways to approach this, depending on the actual needs of your project. First and foremost, I would ask if you actually require a string array as the return type. For most cases, an array is less useful that a List(Of String) or other types which implement IEnumerable.
有多种不同的方法可以解决这个问题,具体取决于项目的实际需要。首先,我会问你是否真的需要一个字符串数组作为返回类型。在大多数情况下,数组不如 List(Of String) 或其他实现 IEnumerable 的类型有用。
Here are two options, both of which involve a List(Of String). However, one returns the List to the caller, which can then choose to employ the many useful methods of the List type in working with the data:
这里有两个选项,都涉及一个 List(Of String)。但是,可以将 List 返回给调用者,然后调用者可以选择使用 List 类型的许多有用方法来处理数据:
THIS is the way I would recommend:
这是我推荐的方式:
Public Function getListOfMotonum() As List(Of String)
Dim SQL As String = "SELECT motonum FROM moto"
Dim output As New List(Of String)()
' Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
Using cn = New SqlConnection(Properties.Settings.[Default].MyConnectionString)
Using cmd = New SqlCommand(SQL, cn)
cn.Open()
Try
Dim dr = cmd.ExecuteReader()
While dr.Read()
output.Add(dr("motonum").ToString())
End While
Catch e As SqlException
' Do some logging or something.
MessageBox.Show("There was an error accessing your data. DETAIL: " & e.ToString())
End Try
End Using
End Using
Return output
End Function
Here is a trivial example of code which consumes the output of this function:
这是一个使用此函数输出的简单代码示例:
Private Sub PrintListToConsole()
Dim MyMotonumList = Me.getListOfMotonum()
For Each item As String In MyMotonumList
Console.WriteLine(item)
Next
End Sub
If your project REQUIRES a string array, the approach may vary. You can return a string from the same function with a couple minor modifications:
如果您的项目需要字符串数组,则方法可能会有所不同。您可以从同一个函数中返回一个字符串,并进行一些小的修改:
' Change the return type in the function signature:
Public Function getArrayOfMotonum() As String()
Dim SQL As String = "SELECT motonum FROM moto"
Dim output As New List(Of String)()
' . . . Same Data Access code as above:
' Just use the .ToArray method of the List class HERE:
Return output.ToArray()
End Function
Or, you can use the same method in your client code, consuming the original function which returns a list:
或者,您可以在客户端代码中使用相同的方法,使用返回列表的原始函数:
Private Sub PrintArrayToConsole()
Dim MyMotonumArray = Me.getArrayOfMotonum()
For Each item As String In MyMotonumArray
Console.WriteLine(item)
Next
End Sub
Returning the List from your function provides a more flexible return type, with many useful methods.
从您的函数返回 List 提供了更灵活的返回类型,以及许多有用的方法。
As a side note, allow me to recommend the Using block when consuming data access resources. This handles the proper tear down and disposal of the Connection and Command objects for you.
作为旁注,请允许我在使用数据访问资源时推荐 Using 块。这会为您处理 Connection 和 Command 对象的正确拆卸和处置。