Quick question that I'm a bit unsure on how to proceed with an array issue.
Basically I have a stored procedure that outputs some course categories and I want to pass all of the rows to an array.
Subject SubjectCode
Applied Science F04
Access to HE F05
So basically the above is an example of what I get from the stored procedure and I'd like to pass the results of the subject to an array.
Dim num as Integer
Dim strNumResult as String
num = 0
Do While (rsData.Read())
num = num + 1
strNumResult = num.ToString()
Dim array(strNumResult) as String
loop
I'm assuming that I'd need to count the amount of rows in the procedure first, then put this result into the array? How would I then systematically pull all subjects in the procedure into the array?
Thanks!
The easiest would be to use a List(Of Subject)
instead which can be resized. I would also create a class for the subject since you have two fields.
Dim subjects = New List(Of Subject)
Using con = New SqlConnection(connectionString)
Using cmd = New SqlCommand("StoredProcedureName", con)
cmd.CommandType = CommandType.StoredProcedure
Try
con.Open()
Using reader = cmd.ExecuteReader()
While reader.Read
Dim subject = reader.GetString(0)
Dim subjectCode = reader.GetString(1)
subjects.Add(New Subject() With {
.Subject = subject,
.SubjectCode = subjectCode
})
End While
End Using
Catch ex As Exception
' log exception
End Try
End Using
End Using
here's a simple implementation of that class:
Public Class Subject
Public Subject As String
Public SubjectCode As String
End Class
If you insist on an array, you can call subjects.ToArray()
.
You need GetRows
method which will get your recordset data into an array:
array = recordset.GetRows(Rows, Start, Fields )
Basically execute your stored procedure and when you have the results use the GetRows method which returns a collection of rows from the result, given an array of unique column ID's.
You can see an example here .
In your case after making sure that you have some records returned you can do this:
arrData = rsData.GetRows()
Hope this helps.
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.