i have several classes that i have created
customer
job
material
truck
each class has a bunch of different properties
what i would like to do is create a generic database connection that i could use to update, retreive, delete, or add to the database.
now the question is how do i do this the best way (with oop in mind)
if i'm retreiving i send the name to the database class and it will pass back one of the objects.
if i'm adding or updating i pass the object to the database class and let it do it's work.
Finally i get the point of the message!!!!!
How do i pass an object to the database class and have it:
recognize what the object is
read each property and also it's value.
i don't know if what i'm asking is worth doing or not but right now i have to create a class(truck,customer,job,etc) and then create a class to handle the db stuff(dbcustomer,dbjob,dbtruck,etc)
i would like to remove the multiple dbclasses.
i think this should work because the tables i have created in the database have the same names and structures as the objects.
thanx for your imput.
(here is some code to help with the descriptions)
just a few button clicks to show how i'm implementing
this is my job object
the dbconnection workhorse
customer
job
material
truck
each class has a bunch of different properties
what i would like to do is create a generic database connection that i could use to update, retreive, delete, or add to the database.
now the question is how do i do this the best way (with oop in mind)
if i'm retreiving i send the name to the database class and it will pass back one of the objects.
if i'm adding or updating i pass the object to the database class and let it do it's work.
Finally i get the point of the message!!!!!
How do i pass an object to the database class and have it:
recognize what the object is
read each property and also it's value.
i don't know if what i'm asking is worth doing or not but right now i have to create a class(truck,customer,job,etc) and then create a class to handle the db stuff(dbcustomer,dbjob,dbtruck,etc)
i would like to remove the multiple dbclasses.
i think this should work because the tables i have created in the database have the same names and structures as the objects.
thanx for your imput.
(here is some code to help with the descriptions)
just a few button clicks to show how i'm implementing
Code:
Private Sub btnJobFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJobFind.Click
StatusBar.Text = "Retreiving Job Names"
Dim Job As New clsJob
Dim JobDB As New DBJob
Job = JobDB.GetJob(txtJobFind.Text)
With Job
Me.txtJobNumber.Text = .JobNumber
Me.txtJobDescription1.Text = .Description(1)
Me.txtJobDescription2.Text = .Description(2)
Me.txtJobDescription3.Text = .Description(3)
Me.txtJobDescription4.Text = .Description(4)
Me.txtJobComment1.Text = .Comment(1)
Me.txtJobComment2.Text = .Comment(2)
Me.txtJobComment3.Text = .Comment(3)
Me.txtJobComment4.Text = .Comment(4)
End With
StatusBar.Text = ""
txtCustomer.Text = ""
Me.txtJobFind.Text = ""
End Sub
Code:
Private Sub btnJobUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJobUpdate.Click
Dim Job As New clsJob
Dim JobDB As New DBJob
With Job
.JobNumber = Me.txtJobNumber.Text
.Description(1) = Me.txtJobDescription1.Text
.Description(2) = Me.txtJobDescription2.Text
.Description(3) = Me.txtJobDescription3.Text
.Description(4) = Me.txtJobDescription4.Text
.Comment(1) = Me.txtJobComment1.Text
.Comment(2) = Me.txtJobComment2.Text
.Comment(3) = Me.txtJobComment3.Text
.Comment(4) = Me.txtJobComment4.Text
End With
JobDB.UpdateJob(Job)
Me.txtJobNumber.Text = ""
Me.txtJobDescription1.Text = ""
Me.txtJobDescription2.Text = ""
Me.txtJobDescription3.Text = ""
Me.txtJobDescription4.Text = ""
Me.txtJobComment1.Text = ""
Me.txtJobComment2.Text = ""
Me.txtJobComment3.Text = ""
Me.txtJobComment4.Text = ""
End Sub
this is my job object
Code:
#Region "Constants"
Private myJobNumber As String
Const MAXDESCRIPTION As Integer = 4
Const MAXCOMMENT As Integer = 4
Const MAX_UDF As Integer = 10
Private myDescription(MAXDESCRIPTION) As String
Private myComment(MAXCOMMENT) As String
Private myUDF(MAX_UDF) As String
#End Region
#Region "Properties"
Public Property JobNumber() As String
Get
Return myJobNumber
End Get
Set(ByVal Value As String)
myJobNumber = Value
End Set
End Property
Public Property Description(ByVal index As Integer) As String
Get
Return myDescription(index)
End Get
Set(ByVal Value As String)
myDescription(index) = Value
End Set
End Property
Public Property Comment(ByVal index As Integer) As String
Get
Return myComment(index)
End Get
Set(ByVal Value As String)
myComment(index) = Value
End Set
End Property
Public Property UDF(ByVal index As Integer) As String
Get
Return myUDF(index)
End Get
Set(ByVal Value As String)
myUDF(index) = Value
End Set
End Property
#End Region
Code:
Public Class DBJob
Public Function GetJob(ByVal JobNumber As String) As clsJob
Try
Dim IDX As Integer = 0
Dim job As New clsJob
Dim Ds As New TestAppDB
Dim Connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11) & "testappdb.mdb"
Dim adapter As New OleDbDataAdapter("SELECT * FROM tblJob WHERE JobNumber LIKE " & _
Chr(34) & "%" & JobNumber & "%" & Chr(34), Connect)
adapter.Fill(Ds, "tblJob")
For IDX = 0 To Ds.tblJob.Rows.Count - 1
Dim idx1 As Integer
For idx1 = 0 To Ds.tblJob.Columns.Count - 1
If Ds.tblJob(IDX).Item(idx1) Is DBNull.Value Then
Ds.tblJob(IDX).Item(idx1) = ""
End If
Next
With job
.JobNumber = Ds.tblJob(IDX).JobNumber
.Description(1) = Ds.tblJob(IDX).Description1
.Description(2) = Ds.tblJob(IDX).Description2
.Description(3) = Ds.tblJob(IDX).Description3
.Description(4) = Ds.tblJob(IDX).Description4
.Comment(1) = Ds.tblJob(IDX).Comment1
.Comment(2) = Ds.tblJob(IDX).Comment2
.Comment(3) = Ds.tblJob(IDX).Comment3
.Comment(4) = Ds.tblJob(IDX).Comment4
.UDF(1) = Ds.tblJob(IDX).UDF1
.UDF(2) = Ds.tblJob(IDX).UDF2
.UDF(3) = Ds.tblJob(IDX).UDF3
.UDF(4) = Ds.tblJob(IDX).UDF4
.UDF(5) = Ds.tblJob(IDX).UDF5
.UDF(6) = Ds.tblJob(IDX).UDF6
.UDF(7) = Ds.tblJob(IDX).UDF7
.UDF(8) = Ds.tblJob(IDX).UDF8
.UDF(9) = Ds.tblJob(IDX).UDF9
.UDF(10) = Ds.tblJob(IDX).UDF10
End With
Next
Ds.Dispose()
adapter.Dispose()
Return job
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Sub UpdateJob(ByVal Job As clsJob)
Try
Dim Connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11) & "testappdb.mdb"
Dim conn As New OleDbConnection(Connect)
Dim adapter As New OleDbDataAdapter
Dim SQLString As String = "SELECT * FROM tblJob WHERE JobNumber = " & Chr(34) & Job.JobNumber & Chr(34)
adapter.SelectCommand = New OleDbCommand(SQLString, conn)
Dim cmdbuilder As New OleDbCommandBuilder(adapter)
conn.Open()
Dim JobUpdate As New TestAppDB
adapter.TableMappings.Add("Table", "tblJob")
adapter.Fill(JobUpdate.Tables("tblJob"))
Dim idx As Integer
For idx = 0 To JobUpdate.tblJob.Rows.Count - 1
With Job
If Job.JobNumber.ToUpper = JobUpdate.tblJob(idx).JobNumber.ToUpper Then
JobUpdate.tblJob(idx).Description1 = .Description(1)
JobUpdate.tblJob(idx).Description2 = .Description(2)
JobUpdate.tblJob(idx).Description3 = .Description(3)
JobUpdate.tblJob(idx).Description3 = .Description(4)
JobUpdate.tblJob(idx).Comment1 = .Comment(1)
JobUpdate.tblJob(idx).Comment2 = .Comment(2)
JobUpdate.tblJob(idx).Comment3 = .Comment(3)
JobUpdate.tblJob(idx).Comment4 = .Comment(4)
JobUpdate.tblJob(idx).UDF1 = .UDF(1)
JobUpdate.tblJob(idx).UDF2 = .UDF(2)
JobUpdate.tblJob(idx).UDF3 = .UDF(3)
JobUpdate.tblJob(idx).UDF4 = .UDF(4)
JobUpdate.tblJob(idx).UDF5 = .UDF(5)
JobUpdate.tblJob(idx).UDF6 = .UDF(6)
JobUpdate.tblJob(idx).UDF7 = .UDF(7)
JobUpdate.tblJob(idx).UDF8 = .UDF(8)
JobUpdate.tblJob(idx).UDF9 = .UDF(9)
JobUpdate.tblJob(idx).UDF10 = .UDF(10)
Dim idx1 As Integer
For idx1 = 0 To JobUpdate.tblJob.Columns.Count - 1
If JobUpdate.tblJob(idx).Item(idx1) Is "" Then
JobUpdate.tblJob(idx).Item(idx1) = DBNull.Value
End If
Next
End If
End With
Next
adapter.Update(JobUpdate)
adapter.Dispose()
JobUpdate.Dispose()
conn.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub