Imports System.Data.SqlClient
Imports Oracle.ManagedDataAccess.Client
Public Class clsDatatabase
    Private Shared MSSQL_inited As Boolean = False
    Public Shared Function ExecuteonOracleDb(CONSTRING As String, ByVal plsqlcommand As String)
        Try
            ' die nötigen Variablen definieren
            Dim result As Object = Nothing
            Dim conn As New OracleConnectionStringBuilder

            Dim Oracle_Conn As OracleConnection = New OracleConnection(CONSTRING)
            Dim Oracle_Command As OracleCommand = Nothing
            Dim DataAdapter As OracleDataAdapter = Nothing

            ' Verbindung zur Datenbank aufbauen
            Try
                Oracle_Conn.Open()
            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.ExecuteonOracleDb(OpenConnection)")
                Return False
            End Try


            ' SQL-Abfrage definieren
            Try
                Oracle_Command = New OracleCommand(plsqlcommand, Oracle_Conn)
                Oracle_Command.AddToStatementCache = True

            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.plsqlcommandDefine")
                Oracle_Conn.Close()
                Return False
            End Try


            ' *** Ausführen des Command ***
            If Command() IsNot Nothing Then
                Try
                    clsLogger.AddDetailLog("Now executing: " & plsqlcommand)
                    Oracle_Command.ExecuteNonQuery()
                    ' DB-Connection schliessen
                    Oracle_Conn.Close()
                    Return True

                Catch ex As Exception
                    clsLogger.Add(ex.Message & vbNewLine & "Execute Command => (" & plsqlcommand & ")", True, "clsDatatabase.ExecuteonOracleDb")
                    Oracle_Conn.Close()

                    Return False
                End Try

            Else
                ' kann eintreten, wenn entweder die SQL-Anweisung falsch ist oder wenn die DataConnection nicht richtig aufgebaut werden konnte
                ' Eintrag in Logdatei machen
                clsLogger.Add("SQL-Command ist ungültig bzw konnte nicht erstellt werden (SQL: " & plsqlcommand & ")", True, "clsDatatabase.ExecuteonOracleDb")
                Return False
            End If


        Catch ex As Exception
            clsLogger.Add("Unexpected Error in ExecuteonOracleDb: " & ex.Message & vbNewLine & "SQL: " & plsqlcommand, True, "clsDatatabase.ExecuteonOracleDb")
            ' an dieser Stelle sollte jeder unvorhergesehene Fehler der Funktion abgefangen werden
            Return False
        End Try

    End Function
    Public Shared Function Oracle_CS_Test(CONSTRING As String)
        Try
            Dim conn As New OracleConnectionStringBuilder

            Dim Oracle_Conn As OracleConnection = New OracleConnection(CONSTRING)

            ' Verbindung zur Datenbank aufbauen
            Try
                Oracle_Conn.Open()

            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.Oracle_CS_Test(OpenConnection)")
                Return False
            End Try
            Oracle_Conn.Close()
            Return True
        Catch ex As Exception
            clsLogger.Add("Unexpected Error in Oracle_CS_Test: " & ex.Message, True, "clsDatatabase.Oracle_CS_Test")
            ' an dieser Stelle sollte jeder unvorhergesehene Fehler der Funktion abgefangen werden
            Return False
        End Try

    End Function
    Public Shared Function MSSQL_CS_Test(CONSTRING As String)
        Try

            ' die nötigen Variablen definieren
            Dim Connection As SqlConnection = Nothing


            ' Verbindung zur Datenbank aufbauen
            Try
                Connection = New SqlConnection(CONSTRING)
                Connection.Open()
            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.MSSQL_CS_Test(OpenConnection)")
                Return False
            End Try
            Connection.Close()
            Return True

        Catch ex As Exception
            ' an dieser Stelle sollte jeder unvorhergesehene Fehler der Funktion abgefangen werden
            clsLogger.Add("UNEXPECTED ERROR: " & ex.Message, True, "clsDatatabase.MSSQL_CS_Test")
            Return False
        End Try

    End Function
    Public Shared Function ExecuteonMSSQL(ConString As String, ByVal sqlcommand As String)
        Try
            If MSSQL_inited = False Then Return False
            ' die nötigen Variablen definieren
            Dim Connection As SqlConnection = Nothing
            ' Dim ConnectionString As SqlConnectionStringBuilder = Nothing
            Dim Command As SqlCommand = Nothing
            Dim DataAdapter As SqlDataAdapter = Nothing

            '' ConnectionString aufbauen (aus Settings auslesen)
            'ConnectionString = New SqlConnectionStringBuilder()
            'ConnectionString.DataSource = datasource
            'ConnectionString.UserID = User
            'ConnectionString.Password = pw
            'ConnectionString.InitialCatalog = init_Cata



            ' Verbindung zur Datenbank aufbauen
            Try
                Connection = New SqlConnection(ConString)
                Connection.Open()
            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.ExecuteonMSSQL(OpenConnection)")
                Return False
            End Try


            ' SQL-Abfrage definieren
            Try
                Command = New SqlCommand(sqlcommand, Connection)
            Catch ex As Exception
                clsLogger.Add(ex.Message, True, "clsDatatabase.ExecuteonMSSQL(DefineCommand)")
                Return False
                Connection.Close()
            End Try

            ' *** Ausführen des Command ***
            If Command IsNot Nothing Then

                Try
                    Command.ExecuteNonQuery()
                    ' DB-Connection schliessen
                    Connection.Close()
                    Return True
                Catch ex As Exception
                    ' bei einem Fehler einen Eintrag in der Logdatei erzeugen
                    clsLogger.Add(ex.Message, True, "clsDatatabase.ExecuteonMSSQL(ExecuteCommand)")
                    Return False
                    Connection.Close()
                End Try
            Else
                ' kann eintreten, wenn entweder die SQL-Anweisung falsch ist oder wenn die DataConnection nicht richtig aufgebaut werden konnte
                ' Eintrag in Logdatei machen
                clsLogger.Add("Could not create COMMAND", True, "clsDatatabase.ExecuteonMSSQL")
                Return False
                Connection.Close()
            End If
        Catch ex As Exception
            ' an dieser Stelle sollte jeder unvorhergesehene Fehler der Funktion abgefangen werden
            clsLogger.Add("UNEXPECTED ERROR: " & ex.Message, True, "clsDatatabase.ExecuteonMSSQL")
            Return False
        End Try

    End Function
    Private Shared connectionString As String
    Public Shared Function Init()
        Try
            connectionString = My.Settings.SQLSERVER_CS
            Dim SQLconnect As New SqlClient.SqlConnection
            SQLconnect.ConnectionString = connectionString
            SQLconnect.Open()
            SQLconnect.Close()
            MSSQL_inited = True
            Return True
        Catch ex As Exception
            clsLogger.Add("Error in DatabaseInit: " & ex.Message, True)
            Return False
        End Try
    End Function



    Public Shared Function Return_Datatable(Select_anweisung As String)
        Try
            If MSSQL_inited = False Then Return Nothing
            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 = 180
            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

            clsLogger.Add("Error in Return_Datatable: " & ex.Message, True)
            clsLogger.Add(">> SQL: " & Select_anweisung, False)

            Return Nothing
        End Try
    End Function
    Public Shared Function Execute_non_Query(ExecuteCMD As String)
        If MSSQL_inited = False Then Return 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.CommandTimeout = 120
            SQLcommand.ExecuteNonQuery()
            SQLcommand.Dispose()
            SQLconnect.Close()



            Return True
        Catch ex As Exception

            clsLogger.Add("Error in Execute_non_Query: " & ex.Message, True)
            clsLogger.Add("SQL: " & ExecuteCMD, False)

            Return False
        End Try
    End Function

    Public Shared Function Execute_Scalar(cmdscalar As String)
        If MSSQL_inited = False Then Return Nothing
        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
            SQLcommand.CommandTimeout = 120
            result = SQLcommand.ExecuteScalar()
            SQLcommand.Dispose()
            SQLconnect.Close()


            Return result
        Catch ex As Exception

            clsLogger.Add("Error in Execute_Scalar: " & ex.Message, True)
            clsLogger.Add("SQL: " & cmdscalar, False)
            Return Nothing
        End Try
    End Function
    Public Shared Function OracleExecute_Scalar(cmdscalar As String, OracleConnection As String)
        Dim result
        Try
            Dim SQLconnect As New OracleConnection
            Dim SQLcommand As New OracleCommand
            SQLconnect.ConnectionString = OracleConnection
            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
            clsLogger.Add("Error in OracleExecute_Scalar: " & ex.Message, True)
            clsLogger.Add("#SQL: " & cmdscalar, False)
            Return Nothing
        End Try
    End Function
    Public Shared Function OracleExecute_non_Query(ExecuteCMD As String, OracleConnection As String, Optional userInput As Boolean = False)
        Try
            Dim SQLconnect As New OracleConnection
            Dim SQLcommand As OracleCommand
            SQLconnect.ConnectionString = OracleConnection
            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

            clsLogger.Add("Error in OracleExecute_non_Query: " & ex.Message, True)
            clsLogger.Add("#SQL: " & ExecuteCMD, False)

            Return False
        End Try
    End Function

    Public Shared Function Oracle_Return_Datatable(Select_anweisung As String, OracleConnection As String, Optional userInput As Boolean = False)
        Try
            Dim SQLconnect As New OracleConnection
            Dim SQLcommand As OracleCommand
            SQLconnect.ConnectionString = OracleConnection
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            SQLcommand.CommandText = Select_anweisung

            Dim adapter1 As OracleDataAdapter = New OracleDataAdapter(SQLcommand)
            Dim dt As DataTable = New DataTable()
            adapter1.Fill(dt)
            SQLconnect.Close()
            Return dt
        Catch ex As Exception

            clsLogger.Add("Error in Oracle_Return_Datatable: " & ex.Message, True)
            clsLogger.Add("#SQL: " & Select_anweisung, False)
            Return Nothing
        End Try
    End Function
End Class
