Question Creating Classes for DB Tables

cadwalli

Active member
Joined
May 27, 2010
Messages
41
Location
United Kingdom
Programming Experience
10+
Hi, I'm new to the forum and pretty new to vb.net so I obviously have a few questions I need to ask to help me along, I hope some of you are able to help.

Firstly, I'm not new to programming, I've been a DB Administrator and programmer in Progress for over 14 years, however I am new to VB.NET and would like some help with regards to understanding DB interaction.

Could someone please explain if my logic/thinking is correct, if I had a table in SQL Server which held purchase order details (po) which had a format such as:

po-num
po-supplier
po-date etc....

Would I then be right in thinking the best way to handle this in VB.NET would be to create a class/object that then maniuplates this, i.e.

Class: POClass

Properties :

POClass.po-num
POClass.po-supplier
POClass.po-date

Methods:

POClass.Load-PO (Loads the po from the db into the relevant properties, i.e. POClass.po-num = po.po-num etc)
POClass.Add-PO (Creates a new purchase order)
POClass.Delete-PO (Deletes the currently loaded po)



OR

Would i simply create load/maniuplation procedures within a form?

My thoughts were that I would go the class/object route for POClass so it could then be added to any other form that wanted to access PO's

Could someone help me understand if I am thinking correctly or if way off the mark here. If anyone has an example of what they think I am trying to do then I would really appreciate it.

I look forward to hearing from you.

Ian
 
Generally speaking, a class that stores the data from the database should not itself interact with the database. Each class represents an entity, either concrete or abstract, and a record in the database represents something completely different to an object that retrieves and saves that data. Look at ADO.NET for an example. The connection, the command, the table and the row are all different classes.
 
Thanks for your reply.

OK, I think this makes sense now, so I dont create a PO class for a PO record. I just use the ADO objects to interact with the data and use it that way.

Thats great, it makes sense (I kind of thought I was off the mark!).

Cheers
Ian
 
I'm not saying that you shouldn't define a class to represent a database record. I'm saying that that class is responsible for representing that record, not for retrieving that record or saving that record.
 
Cheers for the reply.

So I could set up the class to hold the properties for the record, in my example po-num etc, then have methods to manipulate the data in those properties BUT the responsibility of loading the data into the class should be done outside of it (and the folowing write back to db).
 
Follow the DW2 tutorial link in my signature, section Creating a Simple Data App

At the end of it you'll have:
A database
A Dataset containing:
A datatable of datarows - these are classes that represent your DATA
A tableadapter relevant to the datatable - this is a device that pushes data between the datatable and the database


i.e. Visual Studio can create for you in seconds, a good set of classes that will perform the tasks you describe here - it's virtually certain that the code VS writes will be faster, more secure and better compliant with OO principles than code written by a human. Additionally it would take a human days to write what VS creates in seconds
 
Back
Top