Results 1 to 4 of 4

Thread: Must be a better way to do this: Database values and object

  1. #1

    Thread Starter
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375

    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:
    1. ' Person class (In my program these attributes are set using individual property routines to check values/ format etc
    2.  
    3. Public class person
    4.  
    5. public sFname as string
    6. public sLname as string
    7. public nAge as integer
    8. public nWeight as double
    9. public nHeight as double
    10.  
    11. ' Another 30 or so attributes added in below
    12.  
    13. end class
    14.  
    15.  
    16. ' The function below is used to retrieve all the values from a SQL database given a personID number and populates a person object
    17.  
    18. Public function getPerson(byVal nPersonID as integer) as person
    19.  
    20. ' Create the connection object
    21. Dim oConn as new SQLConnection("ConnectionString here")
    22. oCOnn.open
    23. ' COmmand to retrieve all the attributes for a specified person from the database
    24. Dim oCOmmand as new sqlCommand("SELECT * FROM personTable WHERE personID = " & nPersonID,oConn)
    25.  
    26. ' DataReader
    27. Dim oDR as sqlDataReader
    28.  
    29. oDR = oCommand.ExecuteReader()
    30.  
    31. ' Create the person object
    32. Dim oPerson as new clsPerson
    33.  
    34. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    35. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    36. ' 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
    37. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    38. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    39.  
    40. While oDR.Read
    41. oPerson.sFname = oDR("sFName")
    42. oPerson.sLname = oDR("sLName")
    43. etc
    44.  
    45. ' I realise that I could do some coding using a loop and loop though the DataReader items collection kinda like oDR.Item(nLoop
    46. ) and add each item to the person object but I am hoping to learn something new :)
    47.  
    48. End While
    49.  
    50. oConn.close
    51. oCommand.dispose
    52.  
    53. ' Return the person object
    54. return oPerson
    55. End Function

    Thanks in advance
    MarkusJ
    Last edited by MarkusJ_NZ; Nov 23rd, 2003 at 04:41 PM.

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Oracle or maybe even SQL Server 2000 (or maybe Yukon) you can store objects in the database. You might want to fish around in that pool if you have the interest and time.

  3. #3

    Thread Starter
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375
    Thanks for your help
    Cheers
    MarkusJ

  4. #4
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    why not use typed datasets whenever you want to manipulate this object? Then tie the dataset to a datagrid and make changes on the fly!


    Typed dataset is the .net way to translate object models from the database to the client...


    /Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width