Option Strict On

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration



Public Class Database
    Implements IDisposable

#Region "Private Fields"
    'connection to data source
    Private con As SqlConnection
    Private str_con As String = System.Configuration.ConfigurationManager.AppSettings("con_ed1")  'default
    'Error Message field
    Private _errorMessage As String
    'latest exception
    Private _exception As System.Exception
#End Region

#Region "Public Properties"

    Public ReadOnly Property ErrorMessage() As String
        Get
            Return _errorMessage
        End Get
    End Property

    Public ReadOnly Property LastException() As System.Exception
        Get
            Return _exception
        End Get
    End Property

    Public Property ConString() As String
        Get
            Return str_con
        End Get
        Set(ByVal value As String)
            str_con = value
        End Set
    End Property

    Public ReadOnly Property ConnectionString() As String
        Get
            'Return System.Configuration.ConfigurationSettings.AppSettings("Database.DEFAULT_CONNECTION_STRING")
            Return str_con ''System.Configuration.ConfigurationManager.AppSettings("conn")
        End Get
    End Property

#End Region

#Region "Public Method Implementations"

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This function takes a stored proc name and returns an integer '
    'by invoking ExecuteScalar() on the SqlCommand type instance   '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Function RunProcedure(ByVal procName As String) As Integer
        Dim result As Integer
        Dim cmd As SqlCommand
        Try
            cmd = CreateCommand(procName, Nothing)
            result = CType(cmd.ExecuteScalar(), Integer)
        Catch e As Exception
            result = -1
            _errorMessage = e.Message
            _exception = e
        Finally
            Me.Close()
        End Try

        Return result

    End Function

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This function takes a stored proc name, sql parameters returns an integer '
    'by invoking ExecuteScalar() on the SqlCommand type instance               '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Function RunProcedure(ByVal procName As String, ByVal prams As SqlParameter()) As Integer
        Dim result As Integer
        Dim cmd As SqlCommand

        Try

            cmd = CreateCommand(procName, prams)
            result = CInt(cmd.ExecuteScalar())

        Catch e As Exception
            result = -1
            _errorMessage = e.Message
            _exception = e

        Finally
            Me.Close()

        End Try

        Return result

    End Function

    Public Function RunProcstring(ByVal procName As String, ByVal prams As SqlParameter()) As String
        Dim result As String
        Dim cmd As SqlCommand
        Try
            cmd = CreateCommand(procName, prams)
            result = CStr(cmd.ExecuteScalar())
        Catch e As Exception
            result = ""
            _errorMessage = e.Message
            _exception = e
        Finally
            Me.Close()
        End Try
        Return result
    End Function

    Public Function RunProcBool(ByVal procName As String, ByVal prams As SqlParameter()) As Boolean
        Dim result As Boolean
        Dim cmd As SqlCommand
        Try
            cmd = CreateCommand(procName, prams)
            result = CBool(cmd.ExecuteScalar())
        Catch e As Exception
            result = False
            _errorMessage = e.Message
            _exception = e
        Finally
            Me.Close()
        End Try
        Return result
    End Function

    Public Function RunProcBool(ByVal procName As String) As Boolean
        Dim result As Boolean
        Dim cmd As SqlCommand
        Try
            cmd = CreateCommand(procName, Nothing)
            result = CBool(cmd.ExecuteScalar())
        Catch e As Exception
            result = False
            _errorMessage = e.Message
            _exception = e
        Finally
            Me.Close()
        End Try
        Return result
    End Function


    Public Function RunProcDate(ByVal procName As String, ByVal prams As SqlParameter()) As Date
        Dim result As Date
        Dim cmd As SqlCommand

        Try

            cmd = CreateCommand(procName, prams)
            result = CDate(cmd.ExecuteScalar())

        Catch e As Exception
            result = CDate("1000/01/01")
            _errorMessage = e.Message
            _exception = e

        Finally
            Me.Close()

        End Try

        Return result

    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This method takes a stored proc name and a SqlDataReader (BY REF) and returns the results    '
    'in the same DataReader that you pass in as ref. This invokes ExecuteReader on SqlCommand type'
    'instance                                                                                     '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Sub RunProcedure(ByVal procName As String, ByRef dataReader As SqlDataReader)
        Dim cmd As SqlCommand
        Try
            cmd = CreateCommand(procName, Nothing)
            dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

        Catch e As Exception
            dataReader = Nothing
            _errorMessage = e.Message
            _exception = e
            Me.Close()
        End Try
    End Sub

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This method takes a stored proc name, Sql Parameters and a SqlDataReader (BY REF) and         '
    'returns the results in the same DataReader that you pass in as ref. This invokes ExecuteReader'
    'on SqlCommand type instance                                                                   '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Sub RunProcedure(ByVal procName As String, ByVal prams As SqlParameter(), ByRef dataReader As SqlDataReader)
        Dim cmd As SqlCommand
        ''Dim dr As SqlDataReader
        Try
            cmd = CreateCommand(procName, prams)
            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch e As Exception
            dataReader = Nothing
            _errorMessage = e.Message
            _exception = e
            Me.Close()
        End Try

    End Sub

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This method takes a stored proc name, Sql Parameters and a DataSet ByRef      '
    'In case of an exception returns a Nothing and ErrorMessage                    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Sub RunProcedure(ByVal procName As String, ByVal prams As SqlParameter(), ByRef ds As DataSet)
        Dim cmd As SqlCommand
        Dim adapter As SqlDataAdapter
        Try
            cmd = CreateCommand(procName, prams)
            adapter = New SqlDataAdapter(cmd)
            adapter.Fill(ds)
        Catch e As Exception
            _errorMessage = e.Message
            _exception = e
            ds = Nothing
        Finally
            adapter = Nothing
            Me.Close()
        End Try

    End Sub


    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This method takes a stored proc name and a Dataset by Ref     '
    'In case of an exception returns a Nothing and ErrorMessage    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Sub RunProcedure(ByVal procName As String, ByRef ds As DataSet)

        Dim cmd As SqlCommand
        Dim adapter As SqlDataAdapter

        Try
            cmd = CreateCommand(procName, Nothing)
            adapter = New SqlDataAdapter(cmd)
            adapter.Fill(ds)
        Catch e As Exception
            _errorMessage = e.Message
            _exception = e
            ds = Nothing
        Finally
            adapter = Nothing
            Me.Close()
        End Try

    End Sub

    Public Function MakeParameter(ByVal ParamName As String, ByVal Value As Object) As SqlParameter
        Dim param As SqlParameter

        param = New SqlParameter(ParamName, Value)

        Return param

    End Function

    Public Function MakeParameter(ByVal ParamName As String, ByVal Direction As ParameterDirection, Optional ByVal Value As Object = Nothing) As SqlParameter
        Dim param As SqlParameter

        param = New SqlParameter(ParamName, Value)
        param.Direction = Direction

        Return param

    End Function

    Public Function MakeParameter(ByVal ParamName As String, ByVal DBType As SqlDbType, ByVal Size As Integer, ByVal Value As Object) As SqlParameter
        Dim param As SqlParameter

        param = New SqlParameter(ParamName, DBType, Size)
        param.Value = Value

        Return param

    End Function

    Public Sub Close()
        If (con Is Nothing) Then
        Else
            con.Close()
        End If
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose

        If (con Is Nothing) Then
        Else
            con.Dispose()
            con = Nothing
        End If
        'Suppress the finalization process
        System.GC.SuppressFinalize(Me)

    End Sub

#End Region

#Region "Private Method Implementations"

    Private Function CreateCommand(ByVal procName As String, ByVal prams As SqlParameter()) As SqlCommand

        Dim cmd As SqlCommand
        Dim parameter As SqlParameter

        Call Open()

        cmd = New SqlCommand(procName, con)

        cmd.CommandType = CommandType.StoredProcedure

        If (prams Is Nothing) Then
        Else
            For Each parameter In prams
                cmd.Parameters.Add(parameter)
            Next

        End If

        Return cmd

    End Function


    Private Sub Open()
        If (con Is Nothing) Then

            con = New SqlConnection(str_con)
            Try
                con.Open()
            Catch ex As Exception
                _errorMessage = ex.Message
            End Try

        Else
            If ((con.State = ConnectionState.Closed) Or (con.State = ConnectionState.Broken)) Then
                con.Open()
            End If
        End If
    End Sub
#End Region


End Class


