DataSet como RecordSet

Muchas veces, y me ha pasado, nos cuesta despegarnos de modelos de programación adquiridos en lenguajes no .Net
 
Por ejemplo, en Visual Basic 6, donde, he tenido el placer de programar durante mucho tiempo.
 
Este es el caso, donde los datos se manejaban con RecordSets.
 
Actualmente, con DataSets, XML, y demás objetos, muchas veces tenemos un poco de miedo al cambio. Así que, para aquellos que aún quieren trabajar con RecordSets, pero usando DataSets y XML, les coloco una clase que emula las funcionalidades básicas del antiguo objeto, implementando lo nuevo.
 

Imports System.Data

Public

Class RecordSet

Implements IDisposable

#Region "Local Variables"

Private _strPath As String = ""

Private _strCurrentTable As String = ""

Public Enum RecordsetProperties

DischargeChanges

AutoUpdateChanges

End Enum

Private Enum ActionStatus

StandBy

NewRecord

End Enum

Private _EnuStatus As ActionStatus

Private _EnuActions As RecordsetProperties

Private _Ds As DataSet

Private _lngI As Long = -1

Private _Dr As DataRow

#

End Region

#

Region "New Functions"

Public Sub New(ByVal Path As String, ByVal Table As String, ByVal Action As RecordsetProperties)

_strPath = Path

_strCurrentTable = Table

_EnuActions = Action

ResetObject()

End Sub

Public Sub New(ByVal Path As String, ByVal Table As String)

_strPath = Path

_strCurrentTable = Table

_EnuActions = RecordsetProperties.DischargeChanges

ResetObject()

End Sub

Public Sub New(ByVal Path As String)

_strPath = Path

_EnuActions = RecordsetProperties.DischargeChanges

ResetObject()

End Sub

Public Sub New()

_EnuActions = RecordsetProperties.DischargeChanges

ResetObject()

End Sub

#

End Region

#Region "Open Functions"

Public Function Open(ByVal Path As String, ByVal Table As String, ByVal Action As RecordsetProperties) As Boolean

_strPath = Path

_strCurrentTable = Table

_EnuActions = Action

Try

_Ds =

New DataSet

_Ds.ReadXml(_strPath)

ResetObject()

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function Open(ByVal Path As String, ByVal Table As String) As Boolean

_strPath = Path

_strCurrentTable = Table

Try

_Ds =

New DataSet

_Ds.ReadXml(_strPath)

ResetObject()

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function Open(ByVal Table As String) As Boolean

If _strPath.Trim.Length <> 0 Then

_strCurrentTable = Table

If _strCurrentTable.Trim.Length <> 0 Then

Try

_Ds = New DataSet

_Ds.ReadXml(_strPath)

ResetObject()

Return True

Catch ex As Exception

Throw ex

End Try

Else

Throw New Exception("No table name given")

End If

Else

Throw New Exception("No path given")

End If

End Function

Public Function Open() As Boolean

If _strPath.Trim.Length <> 0 Then

If _strCurrentTable.Trim.Length <> 0 Then

Try

_Ds =

New DataSet

_Ds.ReadXml(_strPath)

ResetObject()

Return True

Catch ex As Exception

Throw ex

End Try

Else

Throw New Exception("No table name given")

End If

Else

Throw New Exception("No path given")

End If

End Function

#

End Region

#Region "Common Functions"

Private Sub ResetObject()

_lngI = -1

_EnuStatus = ActionStatus.StandBy

End Sub

#

End Region

#Region "RecordSet handling"

Public Function MoveNext() As Boolean

Try

_lngI += 1

If _lngI < _Ds.Tables(_strCurrentTable).Rows.Count Then

Return True

Else

Return False

End If

Catch ex As Exception

Throw ex

End Try

End Function

Public Function MoveBack() As Boolean

Try

_lngI -= 1

If _lngI >= 0 Then

Return True

Else

_lngI = 0

Return False

End If

Catch ex As Exception

Throw ex

End Try

End Function

Public Function MoveLast() As Boolean

Try

_lngI = _Ds.Tables(_strCurrentTable).Rows.Count – 1

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function MoveFirst() As Boolean

Try

_lngI = 0

Return True

Catch ex As Exception

Throw ex

End Try

End Function

#

End Region

#

Region "Administrative Functions"

Public Function getItem(ByVal ColumnName As String) As String

Try

Return _Ds.Tables(_strCurrentTable).Rows(_lngI).Item(ColumnName)

Catch ex As Exception

Throw ex

End Try

End Function

Public Function getItem(ByVal ColumnIndex As Integer) As String

Try

Return _Ds.Tables(_strCurrentTable).Rows(_lngI).Item(ColumnIndex)

Catch ex As Exception

Throw ex

End Try

End Function

Public Function RecordCount() As Long

Try

Return _Ds.Tables(_strCurrentTable).Rows.Count

Catch ex As Exception

Throw ex

End Try

End Function

Public Function DeleteRecord() As Boolean

Try

_Ds.Tables(_strCurrentTable).Rows(_lngI).Delete()

Select Case _EnuActions

Case RecordsetProperties.AutoUpdateChanges

Save()

End Select

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function UpdateRecord(ByVal ColumnName As String, ByVal Value As String) As Boolean

Try

Select Case _EnuStatus

Case ActionStatus.NewRecord

_Dr(ColumnName) = Value

Return True

Case ActionStatus.StandBy

_Ds.Tables(_strCurrentTable).Rows(_lngI).Item(ColumnName) = Value

Select Case _EnuActions

Case RecordsetProperties.AutoUpdateChanges

Save()

End Select

Return True

End Select

Catch ex As Exception

Throw ex

End Try

End Function

Public Function UpdateRecord(ByVal ColumnIndex As Integer, ByVal Value As String) As Boolean

Try

Select Case _EnuStatus

Case ActionStatus.StandBy

_Ds.Tables(_strCurrentTable).Rows(_lngI).Item(ColumnIndex) = Value

Select Case _EnuActions

Case RecordsetProperties.AutoUpdateChanges

Save()

End Select

Return True

Case ActionStatus.NewRecord

_Dr(ColumnIndex) = Value

Return True

End Select

Catch ex As Exception

Throw ex

End Try

End Function

Public Function FinalizeRecord() As Boolean

Try

If _EnuStatus = ActionStatus.NewRecord Then

_Ds.Tables(_strCurrentTable).Rows.Add(_Dr)

_EnuStatus = ActionStatus.StandBy

Select Case _EnuActions

Case RecordsetProperties.AutoUpdateChanges

Save()

End Select

Return True

Else

Return False

End If

Catch ex As Exception

Throw ex

End Try

End Function

Public Function NewRecord() As Boolean

Try

_Dr = _Ds.Tables(_strCurrentTable).NewRow

_EnuStatus = ActionStatus.NewRecord

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function Save() As Boolean

Try

_Ds.WriteXml(_strPath)

Return True

Catch ex As Exception

Throw ex

End Try

End Function

Public Function SaveAs(ByVal Path As String) As Boolean

Try

_Ds.WriteXml(Path)

Return True

Catch ex As Exception

Throw ex

End Try

End Function

#

End Region

#

Region ".Net Functions"

Public Overridable Sub Dispose() Implements IDisposable.Dispose

Try

_Ds.Dispose()

_Ds =

Nothing

 

Catch ex As Exception

End Try

End Sub

#

End Region

End

Class



Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s