Lewisburg, PA "Next year Raiders in the Super Bowl"
Posts
1,310
Re: SqlClient or DataFactory
Core namespace classes and code:
PART 1 CODE TO LONG FOR MESSAGE FOLLOWING CODE I CANNOT TAKE CREDIT FOR IT IS FROM THE CSLA.NET LIBRARY BY ROCKFORD LHOTKA
VB Code:
Imports System.Data
Namespace Core
<System.Diagnostics.DebuggerStepThrough()> _
Public Class SafeDataReader
Implements IDataReader
Private mDataReader As IDataReader
Protected ReadOnly Property DataReader() As IDataReader
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mDataReader
End Get
End Property
<System.Diagnostics.DebuggerStepThrough()> _
Public Sub New(ByVal dataReader As IDataReader)
mDataReader = dataReader
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetString(ByVal i As Integer) As String _
Implements IDataReader.GetString
If mDataReader.IsDBNull(i) Then
Return ""
Else
Return mDataReader.GetString(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetString(ByVal name As String) As String
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetString(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetValue(ByVal i As Integer) As Object Implements IDataReader.GetValue
If mDataReader.IsDBNull(i) Then
Return Nothing
Else
Return mDataReader.GetValue(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetValue(ByVal name As String) As Object
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetValue(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetInt32(ByVal i As Integer) As Integer Implements IDataReader.GetInt32
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetInt32(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetInt32(ByVal name As String) As Integer
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetInt32(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetDouble(ByVal i As Integer) As Double Implements IDataReader.GetDouble
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetDouble(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetDouble(ByVal name As String) As Double
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetDouble(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetGuid(ByVal i As Integer) As Guid Implements IDataReader.GetGuid
If mDataReader.IsDBNull(i) Then
Return Guid.Empty
Else
Return mDataReader.GetGuid(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetGuid(ByVal name As String) As Guid
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetGuid(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function Read() As Boolean Implements IDataReader.Read
Return mDataReader.Read
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function NextResult() As Boolean Implements IDataReader.NextResult
Return mDataReader.NextResult()
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Sub Close() Implements IDataReader.Close
mDataReader.Close()
End Sub
Public ReadOnly Property Depth() As Integer Implements System.Data.IDataReader.Depth
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mDataReader.Depth
End Get
End Property
Public ReadOnly Property FieldCount() As Integer Implements System.Data.IDataReader.FieldCount
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mDataReader.FieldCount
End Get
End Property
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetBoolean(ByVal i As Integer) As Boolean _
Implements System.Data.IDataReader.GetBoolean
If mDataReader.IsDBNull(i) Then
Return False
Else
Return mDataReader.GetBoolean(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetBoolean(ByVal name As String) As Boolean
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetBoolean(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetByte(ByVal i As Integer) As Byte Implements System.Data.IDataReader.GetByte
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetByte(i)
End If
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetByte(ByVal name As String) As Byte
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetByte(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetBytes(ByVal i As Integer, ByVal fieldOffset As Long, ByVal buffer() As Byte, ByVal bufferOffset As Integer, ByVal length As Integer) As Long Implements System.Data.IDataReader.GetBytes
Public Function GetBytes(ByVal name As String, ByVal fieldOffset As Long, ByVal buffer() As Byte, ByVal bufferOffset As Integer, ByVal length As Integer) As Long
Lewisburg, PA "Next year Raiders in the Super Bowl"
Posts
1,310
Re: SqlClient or DataFactory
Core namespace classes and code:
PART 2 CODE TO LONG FOR MESSAGE FOLLOWING CODE I CANNOT TAKE CREDIT FOR IT IS FROM THE CSLA.NET LIBRARY BY ROCKFORD LHOTKA
VB Code:
<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetChar(ByVal name As String) As Char
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetChar(index)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetChars(ByVal i As Integer, ByVal fieldOffset As Long, ByVal buffer() As Char, ByVal bufferOffset As Integer, ByVal length As Integer) As Long Implements System.Data.IDataReader.GetChars
Public Function GetChars(ByVal name As String, ByVal fieldOffset As Long, ByVal buffer() As Char, ByVal bufferOffset As Integer, ByVal length As Integer) As Long
Private m_Collection As List(Of Core.CSSIDbParameter)
<DebuggerStepThrough()> _
Public Sub New()
m_Collection = New List(Of Core.CSSIDbParameter)
End Sub
<DebuggerStepThrough()> _
Public Sub Add(ByVal item As Core.CSSIDbParameter) Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).Add
m_Collection.Add(item)
End Sub
Public Sub Clear() Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).Clear
m_Collection.Clear()
End Sub
Public Function Contains(ByVal item As Core.CSSIDbParameter) As Boolean Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).Contains
Return m_Collection.Contains(item)
End Function
Public Sub CopyTo(ByVal array() As Core.CSSIDbParameter, ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).CopyTo
m_Collection.CopyTo(array, arrayIndex)
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).Count
Get
Return m_Collection.Count
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).IsReadOnly
Get
Return False
End Get
End Property
Public Function Remove(ByVal item As Core.CSSIDbParameter) As Boolean Implements System.Collections.Generic.ICollection(Of Core.CSSIDbParameter).Remove
m_Collection.Remove(item)
End Function
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Core.CSSIDbParameter) Implements System.Collections.Generic.IEnumerable(Of Core.CSSIDbParameter).GetEnumerator
Return m_Collection.GetEnumerator
End Function
Public Function IndexOf(ByVal item As Core.CSSIDbParameter) As Integer Implements System.Collections.Generic.IList(Of Core.CSSIDbParameter).IndexOf
Return m_Collection.IndexOf(item)
End Function
Public Sub Insert(ByVal index As Integer, ByVal item As Core.CSSIDbParameter) Implements System.Collections.Generic.IList(Of Core.CSSIDbParameter).Insert
m_Collection.Insert(index, item)
End Sub
Default Public Property Item(ByVal index As Integer) As Core.CSSIDbParameter Implements System.Collections.Generic.IList(Of Core.CSSIDbParameter).Item
Get
Return m_Collection(index)
End Get
Set(ByVal value As Core.CSSIDbParameter)
m_Collection(index) = value
End Set
End Property
Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.Generic.IList(Of Core.CSSIDbParameter).RemoveAt
m_Collection.RemoveAt(index)
End Sub
Private Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return Nothing
End Function
Public Function ContainsSelectParameters() As Boolean
Lewisburg, PA "Next year Raiders in the Super Bowl"
Posts
1,310
Re: SqlClient or DataFactory
The ParameterBuilders class removes the need to worry about setting up the parameters that will be passed to a command object. since this class makes use of the CSSIDbParameter, (which makes it so that you have the method AddWithValue), all that is needed is the name of a parameter and the value it will return the parameter to be placed in the command object within the BaseDateAccessLayer class.
Core namespace classes and code:
VB Code:
Imports System.Data
Imports System.Data.Common
Imports System.Text
Namespace Core
Public MustInherit Class ParameterBuilders
Private m_Provider As String
Private m_RegisterPrefix As String = String.Empty
Public WriteOnly Property RegisterPrefex() As String
Set(ByVal value As String)
m_RegisterPrefix = value
End Set
End Property
'Public Shared Function GetBuilders(ByVal provider As String) As ParameterBuilders
' Return New ParameterBuilders(provider)
'End Function
<DebuggerStepThrough()> _
Protected Sub New(ByVal provider As String)
m_Provider = provider
End Sub
Protected Sub New(ByVal provider As String, ByVal prefix As String)
m_Provider = provider
m_RegisterPrefix = prefix
End Sub
<DebuggerStepThrough()> _
Protected Overridable Function ParameterBuilder(ByVal paramName As String, _
ByVal value As Object, _
Optional ByVal prefix As String = "") _
As Core.CSSIDbParameter
Dim hash As New Hashtable
hash.Add(paramName, value)
Return ParameterBuilder(hash, prefix)(0)
End Function
<DebuggerStepThrough()> _
Protected Overridable Function ParameterBuilder(ByVal paramNames As String(), _
ByVal values As Object(), _
Optional ByVal prefix As String = "") _
As Core.CSSIDbParameterCollection
Dim hash As New Hashtable
If paramNames.Length <> values.Length Then
Throw New IndexOutOfRangeException("Total number of parameters and total number of values does not match")
End If
For i As Integer = 0 To paramNames.Length - 1
hash.Add(paramNames(i), values(i))
Next
Return ParameterBuilder(hash, prefix)
End Function
<DebuggerStepThrough()> _
Protected Overridable Function ParameterBuilder(ByVal hash As Hashtable, _
Optional ByVal prefix As String = "") _
As Core.CSSIDbParameterCollection
If String.IsNullOrEmpty(m_RegisterPrefix) Then
If Not String.IsNullOrEmpty(prefix) AndAlso prefix.Length > 1 Then
Throw New OverflowException("Prefix cannot be greater then 1")
ElseIf Not String.IsNullOrEmpty(prefix) AndAlso prefix.Length = 1 AndAlso Not (prefix = "@" OrElse prefix = "?" OrElse prefix = ":") Then
Throw New InvalidExpressionException("Unknown prefix value")
End If
Else
prefix = m_RegisterPrefix
End If
If hash Is Nothing Then Return Nothing
Dim paramsArray As New Core.CSSIDbParameterCollection
Dim paramNames(hash.Count - 1) As String
hash.Keys.CopyTo(paramNames, 0)
For Each name As String In paramNames
With paramsArray
Dim obj As Object = DBNull.Value
If hash(name) IsNot Nothing AndAlso hash(name) IsNot DBNull.Value Then
If hash(name).GetType Is GetType(DateTime) AndAlso hash(name) <> Date.MinValue Then
obj = hash(name)
ElseIf hash(name).GetType IsNot GetType(DateTime) Then