' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Data.OleDb

Namespace DAL
    Public Class AccountData
        Private _name As String
        Private _amount As Decimal
        Public Sub New()
            MyClass.New("", 0D)
        End Sub
        Public Sub New(ByVal name As String, ByVal amount As Decimal)
            _name = name
            _amount = amount
        End Sub
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set
                _name = value
            End Set
        End Property
        Public Property Amount() As Decimal
            Get
                Return _amount
            End Get
            Set
                _amount = value
            End Set
        End Property
    End Class 

    Public Interface AccountManager
        Function Load(ByVal name As String) As AccountData
        Sub Save(ByVal ac As AccountData)
    End Interface 

    Public Class AccountManagerFactory
        Public Shared Function Create(ByVal amtyp As String) As AccountManager
            If amtyp = "DB" Then
                Return New DatabaseAccountManager
            End If
            If amtyp = "XML" Then
                Return New XmlAccountManager
            Else
                Return Nothing
            End If
        End Function
    End Class 

    Friend Class DatabaseAccountManager
    Implements AccountManager
        Public Function Load(ByVal name As String) As AccountData Implements AccountManager.Load
            Dim res As AccountData
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Databases\MSAccess\Test.mdb")
            con.Open
            Dim sel As OleDbCommand = New OleDbCommand("SELECT amount FROM accounts WHERE name='" & name & "'", con)
            Dim rdr As OleDbDataReader = sel.ExecuteReader
            If rdr.Read Then
                Dim amount As Decimal = CType(rdr(0), Decimal)
                res = New AccountData(name, amount)
            Else
                res = Nothing
            End If
            con.Close
            Return res
        End Function
        Public Sub Save(ByVal ac As AccountData) Implements AccountManager.Save
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Databases\MSAccess\Test.mdb")
            con.Open
            Dim upd As OleDbCommand = New OleDbCommand("UPDATE accounts SET amount = " & ac.Amount & " WHERE name='" & ac.Name & "'", con)
            upd.ExecuteNonQuery
        End Sub
    End Class 

    Friend Class XmlAccountManager
    Implements AccountManager
        Private Const XML_FILE As String = "C:\accounts.xml"
        Private doc As XmlDocument
        Public Sub New()
            doc = New XmlDocument
            doc.Load(XML_FILE)
        End Sub
        Public Function Load(ByVal name As String) As AccountData Implements AccountManager.Load
            Dim n As XmlNode = doc.SelectSingleNode("//accounts/account[@name='" & name & "']")
            If Not (n Is Nothing) Then
                Dim amount As Decimal = Decimal.Parse(n.Attributes("amount").Value)
                Return New AccountData(name, amount)
            Else
                Return Nothing
            End If
        End Function
        Public Sub Save(ByVal ac As AccountData) Implements AccountManager.Save
            Dim n As XmlNode = doc.SelectSingleNode("//accounts/account[@name='" & ac.Name & "']")
            n.Attributes("amount").Value = ac.Amount.ToString
            Dim sw As StreamWriter = New StreamWriter(XML_FILE)
            doc.Save(sw)
            sw.Close
        End Sub
    End Class 
End Namespace
