Generic DbProviderFactory Access

Core namespace classes and code:

Namespace Core

<Flags()> _
Public Enum ParameterUsage As Integer
[Select] = 1
[Insert] = 2
[Update] = 4
[Delete] = 8
End Enum

End Namespace

Namespace Core

Public Interface IFetchDataObject

Function InitializeObject(ByVal dr As Core.SafeDataReader)

End Interface

End Namespace
[470 byte] By [vbdotnetboy] at [2007-12-5 10:59:56]
# 1 Re: Generic DbProviderFactory Access
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
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
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetBytes(i, fieldOffset, buffer, bufferOffset, length)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
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
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetBytes(index, fieldOffset, buffer, bufferOffset, length)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetChar(ByVal i As Integer) As Char Implements System.Data.IDataReader.GetChar
If mDataReader.IsDBNull(i) Then
Return Char.MinValue
Else
Dim myChar(0) As Char
mDataReader.GetChars(i, 0, myChar, 0, 1)
Return myChar(0)
End If
End Function
vbdotnetboy at 2007-12-6 10:33:19 >
# 2 Re: Generic DbProviderFactory Access
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

<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
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetChars(i, fieldOffset, buffer, bufferOffset, length)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
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
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetChars(index, fieldOffset, buffer, bufferOffset, length)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetData(ByVal i As Integer) As System.Data.IDataReader Implements System.Data.IDataReader.GetData
Return mDataReader.GetData(i)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetData(ByVal name As String) As System.Data.IDataReader
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetData(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetDataTypeName(ByVal i As Integer) As String Implements System.Data.IDataReader.GetDataTypeName
Return mDataReader.GetDataTypeName(i)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetDataTypeName(ByVal name As String) As String
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetDataTypeName(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetDateTime(ByVal i As Integer) As Date _
Implements System.Data.IDataReader.GetDateTime

If mDataReader.IsDBNull(i) Then
Return Date.MinValue
Else
Return mDataReader.GetDateTime(i)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetDateTime(ByVal name As String) As Date
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetDateTime(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetDecimal(ByVal i As Integer) As Decimal Implements System.Data.IDataReader.GetDecimal
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetDecimal(i)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetDecimal(ByVal name As String) As Decimal
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetDecimal(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetFieldType(ByVal i As Integer) As System.Type Implements System.Data.IDataReader.GetFieldType
Return mDataReader.GetFieldType(i)
End Function

Public Function GetFieldType(ByVal name As String) As System.Type
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetFieldType(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetFloat(ByVal i As Integer) As Single Implements System.Data.IDataReader.GetFloat
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetFloat(i)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetFloat(ByVal name As String) As Single
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetFloat(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetInt16(ByVal i As Integer) As Short Implements System.Data.IDataReader.GetInt16
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetInt16(i)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetInt16(ByVal name As String) As Short
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetInt16(index)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function GetInt64(ByVal i As Integer) As Long Implements System.Data.IDataReader.GetInt64
If mDataReader.IsDBNull(i) Then
Return 0
Else
Return mDataReader.GetInt64(i)
End If
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetInt64(ByVal name As String) As Long
Dim index As Integer = Me.GetOrdinal(name)
Return Me.GetInt64(index)
End Function

Public Overridable Function GetName(ByVal i As Integer) As String Implements System.Data.IDataReader.GetName
Return mDataReader.GetName(i)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetOrdinal(ByVal name As String) As Integer _
Implements System.Data.IDataReader.GetOrdinal

Return mDataReader.GetOrdinal(name)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetSchemaTable() As System.Data.DataTable Implements System.Data.IDataReader.GetSchemaTable
Return mDataReader.GetSchemaTable
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function GetValues(ByVal values() As Object) As Integer Implements System.Data.IDataReader.GetValues
Return mDataReader.GetValues(values)
End Function

Public ReadOnly Property IsClosed() As Boolean Implements System.Data.IDataReader.IsClosed
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mDataReader.IsClosed
End Get
End Property

<System.Diagnostics.DebuggerStepThrough()> _
Public Overridable Function IsDBNull(ByVal i As Integer) As Boolean Implements System.Data.IDataReader.IsDBNull
Return mDataReader.IsDBNull(i)
End Function

<System.Diagnostics.DebuggerStepThrough()> _
Public Function IsDBNull(ByVal name As String) As Boolean
Dim index As Integer = Me.GetOrdinal(name)
Return Me.IsDBNull(index)
End Function

Default Public Overloads ReadOnly Property Item(ByVal name As String) As Object Implements System.Data.IDataReader.Item
<System.Diagnostics.DebuggerStepThrough()> _
Get
Dim value As Object = mDataReader.Item(name)
If DBNull.Value.Equals(value) Then
Return Nothing
Else
Return value
End If
End Get
End Property

Default Public Overridable Overloads ReadOnly Property Item(ByVal i As Integer) As Object Implements System.Data.IDataReader.Item
<System.Diagnostics.DebuggerStepThrough()> _
Get
If mDataReader.IsDBNull(i) Then
Return Nothing
Else
Return mDataReader.Item(i)
End If
End Get
End Property

Public ReadOnly Property RecordsAffected() As Integer Implements System.Data.IDataReader.RecordsAffected
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mDataReader.RecordsAffected
End Get
End Property

#Region " IDisposable Support "

Private disposedValue As Boolean ' To detect redundant calls

Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' free unmanaged resources when explicitly called
mDataReader.Dispose()
End If

' free shared unmanaged resources
End If
Me.disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Protected Overrides Sub Finalize()
Dispose(False)
End Sub

#End Region

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:34:18 >
# 3 Re: Generic DbProviderFactory Access
Core namespace classes and code:

Imports System.Data
Imports System.Text
Namespace Core

Public Class CSSIDbParameter : Inherits Common.DbParameter
'Implements IDbDataParameter

#Region " Fields "

Private m_Usage As Core.ParameterUsage

Private m_IsNullable As Boolean = True
Private m_DbType As DbType = System.Data.DbType.String
Private m_Direction As ParameterDirection = ParameterDirection.Input
Private m_ParameterName As String = String.Empty
Private m_SourceColumn As String = String.Empty
Private m_SourceVersion As DataRowVersion = DataRowVersion.Default
Private m_Percision As Byte = 0
Private m_Scale As Byte = 0
Private m_Size As Integer = 0
Private m_Value As Object = Nothing
Private m_SourceColumnNullMapping As Boolean = False

#End Region

#Region " Constructors "

<DebuggerStepThrough()> _
Public Sub New()
MyBase.New()
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal value As Object)
MyBase.New()
Me.Value = value
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal paramUsage As Core.ParameterUsage)
Me.New()
m_Usage = paramUsage
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal parameterName As String, _
ByVal value As Object, _
Optional ByVal paramUsage As Core.ParameterUsage = ParameterUsage.Select)
Me.New()
Me.ParameterName = parameterName
Me.Value = value
Me.ParameterUsage = paramUsage
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal parameterName As String, _
ByVal dbType As DbType, _
Optional ByVal paramUsage As Core.ParameterUsage = ParameterUsage.Select)
Me.New()
Me.ParameterName = parameterName
Me.DbType = dbType
Me.ParameterUsage = paramUsage
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal parameterName As String, _
ByVal dbType As DbType, _
ByVal size As Integer, _
ByVal sourceColumn As String, _
Optional ByVal paramUsage As Core.ParameterUsage = ParameterUsage.Select)
Me.New()
Me.ParameterName = parameterName
Me.DbType = dbType
Me.Size = size
Me.SourceColumn = sourceColumn
Me.ParameterUsage = paramUsage
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal parameterName As String, _
ByVal dbType As DbType, _
ByVal size As Integer, _
Optional ByVal paramUsage As Core.ParameterUsage = ParameterUsage.Select)
Me.New()
Me.ParameterName = parameterName
Me.DbType = dbType
Me.Size = size
Me.ParameterUsage = paramUsage
End Sub

<DebuggerStepThrough()> _
Public Sub New(ByVal parameterName As String, _
ByVal dbType As DbType, _
ByVal size As Integer, _
ByVal direction As System.Data.ParameterDirection, _
ByVal isNullable As Boolean, _
ByVal percision As Byte, _
ByVal scale As Byte, _
ByVal sourceColumn As String, _
ByVal sourceVersion As System.Data.DataRowVersion, _
ByVal value As Object, _
Optional ByVal paramUsage As Core.ParameterUsage = ParameterUsage.Select)
Me.New()
Me.ParameterName = parameterName
Me.DbType = dbType
Me.Size = size
Me.Direction = direction
m_IsNullable = isNullable
Me.Precision = percision
Me.Scale = scale
Me.SourceColumn = sourceColumn
Me.SourceVersion = sourceVersion
Me.Value = value
Me.ParameterUsage = paramUsage
End Sub

#End Region

Public Property ParameterUsage() As Core.ParameterUsage
<DebuggerStepThrough()> _
Get
Return m_Usage
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Core.ParameterUsage)
m_Usage = value
End Set
End Property

Public Overrides Property DbType() As System.Data.DbType
<DebuggerStepThrough()> _
Get
Return m_DbType
End Get
<DebuggerStepThrough()> _
Set(ByVal value As System.Data.DbType)
m_DbType = value
End Set
End Property

Public Overrides Property Direction() As System.Data.ParameterDirection
<DebuggerStepThrough()> _
Get
Return m_Direction
End Get
<DebuggerStepThrough()> _
Set(ByVal value As System.Data.ParameterDirection)
m_Direction = value
End Set
End Property

Public Overrides Property IsNullable() As Boolean
<DebuggerStepThrough()> _
Get
Return m_IsNullable
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Boolean)
m_IsNullable = value
End Set
End Property

Public Overrides Property ParameterName() As String
<DebuggerStepThrough()> _
Get
Return m_ParameterName
End Get
<DebuggerStepThrough()> _
Set(ByVal value As String)
Dim sb As New StringBuilder(value)
Select Case m_Usage
Case Core.ParameterUsage.Insert
sb.Append("_INSERT")
Case Core.ParameterUsage.Update
sb.Append("_UPDATE")
Case Core.ParameterUsage.Delete
sb.Append("_DELETE")
End Select
m_ParameterName = sb.ToString
End Set
End Property

Public Overrides Property SourceColumn() As String
<DebuggerStepThrough()> _
Get
Return m_SourceColumn
End Get
<DebuggerStepThrough()> _
Set(ByVal value As String)
m_SourceColumn = value
End Set
End Property

Public Overrides Property SourceVersion() As System.Data.DataRowVersion
<DebuggerStepThrough()> _
Get
Return m_SourceVersion
End Get
<DebuggerStepThrough()> _
Set(ByVal value As System.Data.DataRowVersion)
m_SourceVersion = value
End Set
End Property

Public Overrides Property Value() As Object
<DebuggerStepThrough()> _
Get
Return m_Value
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Object)
m_Value = value
End Set
End Property

Public Property Precision() As Byte
<DebuggerStepThrough()> _
Get
Return m_Percision
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Byte)
m_Percision = value
End Set
End Property

Public Property Scale() As Byte
<DebuggerStepThrough()> _
Get
Return m_Scale
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Byte)
m_Scale = value
End Set
End Property

Public Overrides Property Size() As Integer
<DebuggerStepThrough()> _
Get
Return m_Size
End Get
<DebuggerStepThrough()> _
Set(ByVal value As Integer)
m_Size = value
End Set
End Property

Public Overrides Sub ResetDbType()
Return
End Sub

Public Overrides Property SourceColumnNullMapping() As Boolean
Get
Return m_SourceColumnNullMapping
End Get
Set(ByVal value As Boolean)
m_SourceColumnNullMapping = value
End Set
End Property

Public Function Parameter(ByVal provider As String) As System.Data.Common.DbParameter
Dim param As System.Data.Common.DbParameter = _
System.Data.Common.DbProviderFactories.GetFactory(provider).CreateParameter

With param
.DbType = Me.DbType
.Direction = Me.Direction
.ParameterName = Me.ParameterName
.Size = Me.Size
.SourceColumn = Me.SourceColumn
.SourceColumnNullMapping = Me.SourceColumnNullMapping
.SourceVersion = Me.SourceVersion
.Value = Me.Value
End With
Return param
End Function

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:35:19 >
# 4 Re: Generic DbProviderFactory Access
Core namespace classes and code:

Namespace Core

Public Class CSSIDbParameterCollection
Implements Collections.Generic.IList(Of Core.CSSIDbParameter)

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
Return m_Collection.Exists(AddressOf FindSelectParams)
End Function

Public Function ContainsInsertParameters() As Boolean
Return m_Collection.Exists(AddressOf FindInsertParams)
End Function

Public Function ContainsUpdateParameters() As Boolean
Return m_Collection.Exists(AddressOf FindUpdateParams)
End Function

Public Function ContainsDeleteParameters() As Boolean
Return m_Collection.Exists(AddressOf FindDeleteParams)
End Function

Public Function GetAllParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection
End Function

Public Function GetSelectParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection.FindAll(AddressOf FindSelectParams)
End Function

Public Function GetInsertParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection.FindAll(AddressOf FindInsertParams)
End Function

Public Function GetUpdateParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection.FindAll(AddressOf FindUpdateParams)
End Function

Public Function GetDeleteParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection.FindAll(AddressOf FindDeleteParams)
End Function

<DebuggerStepThrough()> _
Public Function GetReturnValueParameter() As Core.CSSIDbParameter
Return m_Collection.Find(AddressOf FindReturnValueParam)
End Function

Public Function GetOutputParameters() As List(Of Core.CSSIDbParameter)
Return m_Collection.FindAll(AddressOf FindOutputParams)
End Function

Private Function FindSelectParams(ByVal param As Core.CSSIDbParameter) As Boolean
If param.ParameterUsage = ParameterUsage.Select Then Return True
Return False
End Function

Private Function FindInsertParams(ByVal param As Core.CSSIDbParameter) As Boolean
If param.ParameterUsage = ParameterUsage.Insert Then Return True
Return False
End Function

Private Function FindUpdateParams(ByVal param As Core.CSSIDbParameter) As Boolean
If param.ParameterUsage = ParameterUsage.Update Then Return True
Return False
End Function

Private Function FindDeleteParams(ByVal param As Core.CSSIDbParameter) As Boolean
If param.ParameterUsage = ParameterUsage.Delete Then Return True
Return False
End Function

<DebuggerStepThrough()> _
Private Function FindReturnValueParam(ByVal param As Core.CSSIDbParameter) As Boolean
If param.Direction = ParameterDirection.ReturnValue Then Return True
Return False
End Function

Private Function FindOutputParams(ByVal param As Core.CSSIDbParameter) As Boolean
If param.Direction = ParameterDirection.Output Then Return True
Return False
End Function

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:36:23 >
# 5 Re: Generic DbProviderFactory Access
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:
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
obj = hash(name)
End If
End If
Dim param As DbParameter = _
DbProviderFactories.GetFactory(m_Provider).CreateParameter

param.ParameterName = String.Format("{0}{1}", prefix, name)
param.Value = obj
.Add(New Core.CSSIDbParameter(param.ParameterName, param.Value))
End With
Next
Return paramsArray
End Function

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:37:30 >
# 6 Re: Generic DbProviderFactory Access
AND FINALLY PART 1
DataLayer namespace classes and code:

Namespace DataLayer
'FUTURE IMPLEMENTATION
Public Class FetchDataEventArgs : Inherits System.EventArgs

Public Sub New()

End Sub

End Class

End Namespace

Namespace DataLayer
'FUTURE IMPLEMENTATION
Public Class TransmitDataEventArgs : Inherits System.EventArgs

Public Sub New()

End Sub

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:38:28 >
# 7 Re: Generic DbProviderFactory Access
The Last Bit I'm Posting As A Zip File As I Can't Find A Happy Medium
This Code Also Needs A Major Refactoring Which I'm Going To Do Someday
vbdotnetboy at 2007-12-6 10:39:23 >
# 8 Re: Generic DbProviderFactory Access
EXAMPLE OF HOW IT ALL TIES TOGETHER

Imports System.Text
Imports CSSI.Library.Data.Core
Imports System.ComponentModel
Imports CSSI.Configuration.ConnectionStringExSectionHandler
Imports CSSI.Configuration.ConnectionStringExSectionHandler.ConnectionStringExSection
Namespace Security.ChallengeQuestion

<Serializable()> _
Public Class QuestionManager : Inherits CSSI.Library.Data.DataLayer.BaseDataAccessLayer

Public Sub New(ByVal dataProvider As String, ByVal cnnStr As String)
MyBase.New(dataProvider, cnnStr)
End Sub

Public Sub New()
MyBase.New(GetConfig.ConnectionStrings(0).DataProvider, _
GetConfig.ConnectionStrings(0).ConnectionString)
End Sub

<DebuggerStepThrough()> _
<DataObjectMethod(DataObjectMethodType.Select)> _
Public Function GetQuestionsList() As List(Of Question)
Dim list As List(Of Question) = Nothing
Dim sql As New StringBuilder
Dim paramHash As New Hashtable

With sql
.AppendFormat("SELECT * FROM {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.AppendFormat("WHERE {0}=@{0}", DB_Constants.ChallengeQuestions.ACTIVE)
End With

With paramHash
.Add(DB_Constants.ChallengeQuestions.ACTIVE, 1)
End With

For Each obj As Object In Me.FetchDataObjects(GetType(Question), sql.ToString, Me.ParameterBuilder(paramHash, "@"), , True)
If list Is Nothing Then
list = New List(Of Question)
End If
list.Add(obj)
Next
Return list
End Function

<DebuggerStepThrough()> _
<DataObjectMethod(DataObjectMethodType.Select)> _
Public Function GetQuestionsList(ByVal selectedQuestionId As Int32) As List(Of Question)
Dim list As List(Of Question) = Nothing
Dim sql As New StringBuilder
Dim paramHash As New Hashtable

With sql
.AppendFormat("SELECT * FROM {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.AppendFormat("WHERE {0}=@{0} AND {1}<>@{1}", _
DB_Constants.ChallengeQuestions.ACTIVE, _
DB_Constants.ChallengeQuestions.QUESTION_ID)
End With

With paramHash
.Add(DB_Constants.ChallengeQuestions.ACTIVE, 1)
.Add(DB_Constants.ChallengeQuestions.QUESTION_ID, selectedQuestionId)
End With

For Each obj As Object In Me.FetchDataObjects(GetType(Question), sql.ToString, Me.ParameterBuilder(paramHash, "@"), , True)
If list Is Nothing Then
list = New List(Of Question)
End If
list.Add(obj)
Next
Return list
End Function

<DebuggerStepThrough()> _
<DataObjectMethod(DataObjectMethodType.Select)> _
Public Function GetQuestionsList(ByVal selectedQuestionId1 As Int32, _
ByVal selectedQuestionId2 As Int32) As List(Of Question)
Dim list As List(Of Question) = Nothing
Dim sql As New StringBuilder
Dim paramHash As New Hashtable

With sql
.AppendFormat("SELECT * FROM {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.AppendFormat("WHERE {0}=@{0} AND {1}<>@{1}_1 AND {1}<>@{1}_2", _
DB_Constants.ChallengeQuestions.ACTIVE, _
DB_Constants.ChallengeQuestions.QUESTION_ID)
End With

With paramHash
.Add(DB_Constants.ChallengeQuestions.ACTIVE, 1)
.Add(DB_Constants.ChallengeQuestions.QUESTION_ID + "_1", selectedQuestionId1)
.Add(DB_Constants.ChallengeQuestions.QUESTION_ID + "_2", selectedQuestionId2)
End With

For Each obj As Object In Me.FetchDataObjects(GetType(Question), sql.ToString, Me.ParameterBuilder(paramHash, "@"), , True)
If list Is Nothing Then
list = New List(Of Question)
End If
list.Add(obj)
Next
Return list
End Function

<DataObjectMethod(DataObjectMethodType.Select)> _
Public Function GetQuestion(ByVal questionId As Int32) As Question
Dim quest As Question = Nothing
Dim sql As New StringBuilder
Dim paramHash As New Hashtable

With sql
.AppendFormat("SELECT * FROM {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.AppendFormat("WHERE {0}=@{0}", DB_Constants.ChallengeQuestions.QUESTION_ID)
End With
With paramHash
.Add(DB_Constants.ChallengeQuestions.QUESTION_ID, questionId)
End With

quest = Me.FetchDataObject(GetType(Question), sql.ToString, _
Me.ParameterBuilder(paramHash, "@"), , True)

Return quest
End Function

<DataObjectMethod(DataObjectMethodType.Select)> _
Public Function GetQuestions() As QuestionTable
Dim sql As New StringBuilder
With sql
.AppendFormat("SELECT * FROM {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
End With
Dim tbl As QuestionTable = Me.FetchData(New QuestionTable, sql.ToString, Nothing)

Return tbl
End Function

<DataObjectMethod(DataObjectMethodType.Update Or DataObjectMethodType.Insert)> _
Public Function Save(ByVal question As Question) As Int32
Dim retval As Int32 = -1
Dim sql As New StringBuilder
Dim paramHash As New Hashtable

With sql
If question.QuestionId < 1 Then
.AppendFormat("INSERT INTO {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.AppendFormat("({0},{1},{2}) VALUES(@{0},@{1},@{2})", _
DB_Constants.ChallengeQuestions.QUESTION, _
DB_Constants.ChallengeQuestions.TYPE, _
DB_Constants.ChallengeQuestions.ACTIVE)
Else
.AppendFormat("UPDATE {0}", DB_Constants.ChallengeQuestions.TABLE_NAME)
.AppendLine()
.Append("SET")
.AppendLine()
.AppendFormat("{0}=@{0},{1}=@{1},{2}=@{2}", _
DB_Constants.ChallengeQuestions.QUESTION, _
DB_Constants.ChallengeQuestions.TYPE, _
DB_Constants.ChallengeQuestions.ACTIVE)
.AppendLine()
.AppendFormat("WHERE {0}=@{0}", DB_Constants.ChallengeQuestions.QUESTION_ID)
End If
End With
With paramHash
If question.QuestionId > 0 Then
.Add(DB_Constants.ChallengeQuestions.QUESTION_ID, question.QuestionId)
End If
.Add(DB_Constants.ChallengeQuestions.QUESTION, question.Question)
.Add(DB_Constants.ChallengeQuestions.TYPE, question.Type)
.Add(DB_Constants.ChallengeQuestions.ACTIVE, IIf(question.Active, "1", "0"))
End With
Dim trx As Common.DbTransaction = Nothing
retval = Me.TransmitData(sql.ToString, Me.ParameterBuilder(paramHash, "@"), , , True, trx)

If retval = 1 Then
trx.Commit()
Else
trx.Rollback()
End If
trx.Dispose()
Return retval
End Function

End Class

End Namespace
vbdotnetboy at 2007-12-6 10:40:26 >
# 9 Re: Generic DbProviderFactory Access
connectionstring configuration section code
vbdotnetboy at 2007-12-6 10:41:34 >
# 10 Re: Generic DbProviderFactory Access
Moved to the CodeBank
Hack at 2007-12-6 10:42:35 >