Strongly Typed DataReaders for VB.Net

I wanted to finish this presentation code before posting, but it is taking too long. So here is my presentation code for the strongly type datareader and command object base classes.

I’ll post sample code later.

Imports System
Imports System.Data.SqlClient
Imports System.Xml

Public MustInherit Class GenericReaderEx
Implements IDisposable
Private cm As GenericCommandEx
Private dr As SqlDataReader
Public Sub New(ByVal cmd As GenericCommandEx)
MyBase.New()
cm = cmd
dr = cmd.ExecuteDataReader
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
dr.Close()
cm.Dispose()
End Sub
Public Function Read() As Boolean
Return dr.Read
End Function
Public Function NextResultset() As Boolean
Return dr.NextResult
End Function
Public Function RecordAffected() As Integer
Return dr.RecordsAffected
End Function
Public Function IsClosed() As Boolean
Return dr.IsClosed
End Function
Public Sub Close()
dr.Close()
End Sub
Public Function ToOrd(ByVal FieldName As String) As Integer
Return dr.GetOrdinal(FieldName)
End Function
Public Function ToGuid(ByVal FieldName As String) As String
Return dr.GetGuid(ToOrd(FieldName)).ToString
End Function
Public Overloads Function ToString(ByVal FieldName As String) As String
Return dr.GetString(ToOrd(FieldName))
End Function
Public Overloads Function ToByte(ByVal FieldName As String) As Byte
Return dr.GetByte(ToOrd(FieldName))
End Function
Public Overloads Function ToShort(ByVal FieldName As String) As Short
Return dr.GetInt16(ToOrd(FieldName))
End Function
Public Overloads Function ToInteger(ByVal FieldName As String) As Integer
Return dr.GetInt32(ToOrd(FieldName))
End Function
Public Overloads Function ToLong(ByVal FieldName As String) As Long
Return dr.GetInt64(ToOrd(FieldName))
End Function
Public Function ToDecimal(ByVal FieldName As String) As Decimal
Return dr.GetDecimal(ToOrd(FieldName))
End Function
Public Function Toint32(ByVal FieldName As String) As Integer
Return dr.GetInt32(ToOrd(FieldName))
End Function
Public Function IsDBNull(ByVal FieldName As String) As Boolean
Return dr.IsDBNull(ToOrd(FieldName))
End Function
End Class

#Region “Select Command”
Public MustInherit Class GenericCommandEx
Implements IDisposable
Protected _cn As SqlClient.SqlConnection
Protected _cm As SqlClient.SqlCommand
Public Sub New(ByVal Cn As SqlClient.SqlConnection)
MyBase.New()
_cn = Cn
_cm = _cn.CreateCommand
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
_cm.Dispose()
_cn.Close()
_cn.Dispose()
End Sub
Public Function ExecuteDataReader() As SqlDataReader
SetParams()
Return _cm.ExecuteReader
End Function
Public Function ExecuteDataReader(ByVal commandBehavior As CommandBehavior) As SqlDataReader
SetParams()
Return _cm.ExecuteReader(commandBehavior)
End Function
Public Function ExecuteNonQuery() As Integer
SetParams()
Return _cm.ExecuteNonQuery
End Function
Public Function ExecuteScalar() As Object
SetParams()
Return _cm.ExecuteScalar
End Function
Public Function ExecuteXmlReader() As Xml.XmlReader
SetParams()
Return _cm.ExecuteXmlReader
End Function
Public MustOverride Sub SetParams()
Protected MustOverride ReadOnly Property SQLString() As String
Public MustOverride Function CreateSqlstringCommand(ByRef sqlConnection As SqlConnection) As SqlCommand
End Class
#End Region

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.