Must be a better way to do this: Database values and object
Hi, there must be a better way to do the following ;)
I have a person object which contains about 40 different properties (first name, last name, height, age, weight etc)
Each of these properties is captured from a form and then stored into a database, that part is fine.
When someone wants to edit a person I first create the person object and then retrieve the property values from the database where they are stored.
Currently I am looking at just creating a routine that uses a datareader and then sets the individual attributes
pseudo code below
VB Code:
' Person class (In my program these attributes are set using individual property routines to check values/ format etc
Public class person
public sFname as string
public sLname as string
public nAge as integer
public nWeight as double
public nHeight as double
' Another 30 or so attributes added in below
end class
' The function below is used to retrieve all the values from a SQL database given a personID number and populates a person object
Public function getPerson(byVal nPersonID as integer) as person
' Create the connection object
Dim oConn as new SQLConnection("ConnectionString here")
oCOnn.open
' COmmand to retrieve all the attributes for a specified person from the database
Dim oCOmmand as new sqlCommand("SELECT * FROM personTable WHERE personID = " & nPersonID,oConn)
' DataReader
Dim oDR as sqlDataReader
oDR = oCommand.ExecuteReader()
' Create the person object
Dim oPerson as new clsPerson
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' What I am after, is there a better way using ADO.Net to retrieve all the values from a database table and store them in a object. The object has the exact same number of attributes as the table in which the attributes are stored in and the object proprtry names are exactly the same as the attribute names in the database
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While oDR.Read
oPerson.sFname = oDR("sFName")
oPerson.sLname = oDR("sLName")
etc
' I realise that I could do some coding using a loop and loop though the DataReader items collection kinda like oDR.Item(nLoop
) and add each item to the person object but I am hoping to learn something new :)
End While
oConn.close
oCommand.dispose
' Return the person object
return oPerson
End Function
Thanks in advance
MarkusJ