Implementing OOP to Develop Database Oriented Applications using VB.NET 2005 - Developing a factory class to update rows to the database (Page 4 of 6 )
Once we are provided with every object, we need to update them in the database whenever necessary. The following is the class which tries to save the object in the database as needed:
PublicClass EmployeeFactory
Inherits Employee
Public Sub New()
End Sub
Public Sub New(ByVal e As Employee)
MyBase.New(e)
End Sub
Public Sub add()
DBHelper.SQLExecute("insert into emp(empno,ename,sal,deptno) values (" & Empno & ",'" & Ename & "'," & Sal & "," & Deptno & ")")
End Sub
Public Sub add(ByVal e As Employee)
Empno = e.Empno
Ename = e.Ename
Sal = e.Sal
Deptno = e.Deptno
add()
End Sub
Public Sub update()
DBHelper.SQLExecute("update emp set ename='" & Ename & "',sal=" & Sal & ",deptno=" & Deptno & " where empno=" & Empno)
End Sub
Public Sub update(ByVal e As Employee)
Empno = e.Empno
Ename = e.Ename
Sal = e.Sal
Deptno = e.Deptno
update()
End Sub
Public Sub delete()
DBHelper.SQLExecute("delete from emp where empno=" & Empno)
End Sub
Public Sub delete(ByVal empno As Integer)
Me.Empno = empno
delete()
End Sub
Public Function getEmployee(ByVal empno As Integer) As Employee
Dim dr As DataRow = DBHelper.getRow("select * from emp where empno=" & empno)
empno = dr("empno")
Ename = dr("ename")
Sal = dr("sal")
Deptno = dr("deptno")
Return CType(Me, Employee)
End Function
Public Shared Function getList() As Employees
Dim dt As DataTable = DBHelper.getDataTable("select * from emp")
Dim colEmp As New Employees
For Each dr As DataRow In dt.Rows
colEmp.add(New Employee(dr("empno"), dr("ename"), dr("sal"), dr("deptno")))
Next
Return colEmp
End Function
EndClass
Next: Developing a class to interact with databases >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee