Imports DigitalData.EMLProfiler.ClassCurrent
Imports DigitalData.Modules.Logging
Public Class clsDatabase
    Public connectionString As String
    Private Logger As Logger
    Private MyLogger As LogConfig

    Sub New(LogConf As LogConfig, ConStr As String)
        Logger = LogConf.GetLogger
        MyLogger = LogConf
        Init(ConStr)
    End Sub
    Public Function Init(ConString As String)
        Try

            Dim SQLconnect As New SqlClient.SqlConnection
            SQLconnect.ConnectionString = ConString
            SQLconnect.Open()
            SQLconnect.Close()
            connectionString = ConString
            DTCONFIG = Return_Datatable("select * from TBEMLP_CONFIG")
            If Not IsNothing(DTCONFIG) Then
                If DTCONFIG.Rows.Count = 1 Then
                    PATH_TEMP = DTCONFIG.Rows(0).Item("PATH_EMAIL_TEMP")
                    PATH_ERROR = DTCONFIG.Rows(0).Item("PATH_EMAIL_ERRORS")
                    MAIL_BODY_FONT = DTCONFIG.Rows(0).Item("FONT_BODY")
                    WM_CON_STRING = DTCONFIG.Rows(0).Item("WM_CON_STRING")
                    TIMER_INTERVALL = DTCONFIG.Rows(0).Item("CHECK_INTERVALL_MINUTES")
                End If
            End If
            DT_ECM_BASE_CONFIG = Return_Datatable("select * from TBDD_BASECONFIG")
            Try
                WM_DRIVE = DT_ECM_BASE_CONFIG.Rows(0).Item("WM_DRIVE")
            Catch ex As Exception
                Logger.Error("WM_DRIVE not part of DT_ECM_BASE_CONFIG: " & ex.Message)
                WM_DRIVE = "W"
            End Try
            Logger.Debug("Database initialized")
            Return True
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in DatabaseInit: " & ex.Message)
            Return False
        End Try
    End Function

    Public Function Return_Datatable(Select_anweisung As String)
        Try
            Dim SQLconnect As New SqlClient.SqlConnection
            Dim SQLcommand As SqlClient.SqlCommand
            SQLconnect.ConnectionString = connectionString
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            SQLcommand.CommandText = Select_anweisung
            SQLcommand.CommandTimeout = 240

            Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQLcommand)

            Dim dt As DataTable = New DataTable()
            Logger.Debug("ReturnDatatable Command: " & Select_anweisung)
            adapter1.Fill(dt)
            SQLconnect.Close()
            Return dt
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in Return_Datatable: " & ex.Message)
            Logger.Warn("SQL: " & Select_anweisung)
            Return Nothing
        End Try
    End Function
    Public Function Return_DatatableCS(Select_anweisung As String, CS As String)
        Try
            Dim SQLconnect As New SqlClient.SqlConnection
            Dim SQLcommand As SqlClient.SqlCommand
            SQLconnect.ConnectionString = CS
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            SQLcommand.CommandText = Select_anweisung
            SQLcommand.CommandTimeout = 240

            Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQLcommand)

            Dim dt As DataTable = New DataTable()
            adapter1.Fill(dt)
            SQLconnect.Close()
            Return dt
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in Return_DatatableCS: " & ex.Message)
            Logger.Warn("SQL: " & Select_anweisung)
            Return Nothing
        End Try
    End Function
    Public Function Execute_non_Query(ExecuteCMD As String, Optional Userinput As Boolean = False)
        Try
            Dim SQLconnect As New SqlClient.SqlConnection
            Dim SQLcommand As SqlClient.SqlCommand
            SQLconnect.ConnectionString = connectionString
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = ExecuteCMD
            SQLcommand.ExecuteNonQuery()
            SQLcommand.Dispose()
            SQLconnect.Close()
            Return True
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in Execute_non_Query: " & ex.Message)
            Logger.Warn("SQL: " & ExecuteCMD)
            Return False
        End Try
    End Function
    Public Function Execute_Scalar(cmdscalar As String)
        Dim result
        Try
            Dim SQLconnect As New SqlClient.SqlConnection
            Dim SQLcommand As SqlClient.SqlCommand
            SQLconnect.ConnectionString = connectionString
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = cmdscalar
            result = SQLcommand.ExecuteScalar()
            SQLcommand.Dispose()
            SQLconnect.Close()
            Return result
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in Execute_Scalar: " & ex.Message)
            Logger.Warn("SQL: " & cmdscalar)
            Return Nothing
        End Try
    End Function
    Public Function Execute_Scalar_CS(cmdscalar As String, constring As String)
        Dim result
        Try
            Dim SQLconnect As New SqlClient.SqlConnection
            Dim SQLcommand As SqlClient.SqlCommand
            SQLconnect.ConnectionString = constring
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = cmdscalar
            result = SQLcommand.ExecuteScalar()
            SQLcommand.Dispose()
            SQLconnect.Close()
            Return result
        Catch ex As Exception
            Logger.Error(ex)
            Logger.Warn("Unexpected Error  in Execute_Scalar_CS: " & ex.Message)
            Logger.Warn("SQL: " & cmdscalar)
            Return Nothing
        End Try
    End Function
End Class
