What I would like to do is to have a class that has the fields:
- ID (integer)
- Fnamn (string)
- Enamn (string)
- Fdatum (date)
and put each post from the DB in one such object. Then I want to put all these objects either in one object that holds all objects (which I believe is the better solution) OR in an array of objects. Then I want the function to return this object of objects (or array of objects).
I have tried a lot of different (obviously incorrect) ways of doing this and have not succseeded, thus the returning of a string.
I have a module named module1 where I put definitions that are needed in different parts of the app, I've tried to put a class definition in it. (code below).
The function that fetches posts:
Public Function ListaPersoner(ByVal enamn As String, ByVal e As System.EventArgs) As String Dim cno As New SqlConnection Dim cmd As New SqlCommand Dim rdr As SqlDataReader Dim rad As String Dim ant As Integer Dim command As New SqlCommand cno.ConnectionString = connString1 cmd.Connection = cno cmd.Parameters.Add("@Enamn", SqlDbType.NChar, 10) cmd.Parameters("@Enamn").Value = enamn If enamn.Trim = "" Then cmd.CommandText = "SELECT * FROM namn ORDER BY Fnamn" Else cmd.CommandText = "SELECT * FROM namn where enamn like @Enamn ORDER BY Fnamn" End If cno.Open() ant = 0 rad = "" rdr = cmd.ExecuteReader While rdr.Read ant += 1 rad &= rdr.Item("ID").ToString.Trim & " " rad &= rdr.Item("Fnamn").ToString.Trim & " " rad &= rdr.Item("Enamn").ToString.Trim & " " rad &= rdr.Item("FDatum").ToString.Trim & vbCrLf End While Return rad End Function
A kind of class definition (totally wrong?):
Public Class personClass Public _Identitet As Integer Public _FörNamn As String Public _EfterNamn As String Public _Födelse As SqlTypes.SqlDateTime End Class
How do I make the function return a bunch of objects that has the fields that are in the personClass above?
PS: Any improvements of the supplied code will be appreciated (e.g. try-catch-finally).
PPS: I do not want to use data set and/or datagridwiew at this time.
This post has been edited by jens: 26 October 2008 - 06:43 AM