Imports Oracle.ManagedDataAccess.Client
Public Class frmConnections

    Private Sub TBDD_CONNECTIONBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Try
            Me.Validate()
            Me.TBDD_CONNECTIONBindingSource.EndEdit()
            If Me.MyDataset.TBDD_CONNECTION.GetChanges Is Nothing = False Then
                Me.GEANDERTWERTextBox.Text = Environment.UserName
                Me.TBDD_CONNECTIONBindingSource.EndEdit()
                TBDD_CONNECTIONTableAdapter.Update(MyDataset.TBDD_CONNECTION)
                Me.labelStatus.Caption = "Verbindung gespeichert!"
            Else
                Me.labelStatus.Caption = String.Empty
            End If
        Catch ex As Exception
            MsgBox("Error in Save Connection:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub frmConnections_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "MyDataset.TBDD_CONNECTION". Sie können sie bei Bedarf verschieben oder entfernen.
        Me.TBDD_CONNECTIONTableAdapter.Connection.ConnectionString = MyConnectionString

    End Sub
    Sub Load_Conns()
        Try
            Me.TBDD_CONNECTIONTableAdapter.Fill(Me.MyDataset.TBDD_CONNECTION)
        Catch ex As Exception
            MsgBox("Error in Laod Form: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub SQL_PROVIDERComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SQL_PROVIDERComboBox.SelectedIndexChanged
        If Me.SQL_PROVIDERComboBox.SelectedIndex <> -1 Then
            CheckBoxwin_aut.Visible = False
            chkOR_ohne_TNS.Visible = False
            Select Case Me.SQL_PROVIDERComboBox.SelectedItem

                Case "MS-SQL".ToUpper
                    If USER_LANGUAGE = "de-DE" Then
                        Me.lblDataSource.Text = "Datenbank-Server (IP-Adresse oder Name)*:"
                        Me.lblInitialCatalog.Text = "Datenbank-Name:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Passwort:"
                    Else
                        Me.lblDataSource.Text = "Database-Server (IP-Adress or Name)*:"
                        Me.lblInitialCatalog.Text = "Database-Name:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Passwort:"
                    End If

                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""

                    Me.lblInitialCatalog.Visible = False
                    Me.DATENBANKTextBox.Visible = False
                    Me.cmbInitialCatalog.Visible = True
                    Me.lblDatenbank.Visible = True
                    CheckBoxwin_aut.Visible = True
                Case "MS-SQLServer".ToUpper
                    If USER_LANGUAGE = "de-DE" Then
                        Me.lblDataSource.Text = "Datenbank-Server (IP-Adresse oder Name)*:"
                        Me.lblInitialCatalog.Text = "Datenbank-Name:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Passwort:"
                    Else
                        Me.lblDataSource.Text = "Database-Server (IP-Adress or Name)*:"
                        Me.lblInitialCatalog.Text = "Database-Name:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Passwort:"
                    End If

                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""

                    Me.lblInitialCatalog.Visible = False
                    Me.DATENBANKTextBox.Visible = False
                    Me.cmbInitialCatalog.Visible = True
                    Me.lblDatenbank.Visible = True
                    CheckBoxwin_aut.Visible = True

                Case "Oracle"
                    If USER_LANGUAGE = "de-DE" Then
                        Me.lblDataSource.Text = "IP Oracle-Server:"
                        Me.lblInitialCatalog.Text = "Servicename:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Passwort:"
                    Else
                        Me.lblDataSource.Text = "IP Oracle-Server:"
                        Me.lblInitialCatalog.Text = "Servicename:"
                        Me.lblUserId.Text = "UserId:"
                        Me.lblPassword.Text = "Password:"
                    End If

                    Me.cmbInitialCatalog.Visible = False
                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""
                    Me.lblInitialCatalog.Visible = True
                    Me.DATENBANKTextBox.Visible = True
                    chkOR_ohne_TNS.Visible = True
            End Select

            Me.lblDataSource.Visible = True
            Me.lblUserId.Visible = True
            Me.lblPassword.Visible = True
            Me.SERVERTextBox.Visible = True
            Me.USERNAMETextBox.Visible = True
            Me.PASSWORDTextBox.Visible = True
        Else
            Me.lblDataSource.Visible = False
            Me.lblUserId.Visible = False
            Me.lblPassword.Visible = False
            Me.SERVERTextBox.Visible = False
            Me.USERNAMETextBox.Visible = False
            Me.PASSWORDTextBox.Visible = False
        End If
    End Sub

    Private Sub CheckBoxwin_aut_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxwin_aut.CheckedChanged
        If CheckBoxwin_aut.Checked Then
            Me.USERNAMETextBox.Enabled = False
            Me.PASSWORDTextBox.Enabled = False
            Me.USERNAMETextBox.Text = "WINAUTH"
            Me.PASSWORDTextBox.Text = ""
        Else
            Me.USERNAMETextBox.Enabled = True
            Me.PASSWORDTextBox.Enabled = True
            Me.USERNAMETextBox.Text = ""
            Me.PASSWORDTextBox.Text = ""
        End If
    End Sub

    Private Sub chkOR_ohne_TNS_CheckedChanged(sender As Object, e As EventArgs) Handles chkOR_ohne_TNS.CheckedChanged
        If chkOR_ohne_TNS.Checked Then
            Me.BEMERKUNGTextBox.Text = "without tnsnames"
        Else
            Me.BEMERKUNGTextBox.Text = ""
        End If
    End Sub

    Private Sub btnCheck_Con_Click(sender As Object, e As EventArgs)
        Dim msg_string As String
        Try
            If Me.SQL_PROVIDERComboBox.SelectedIndex <> -1 Then
                Cursor = Cursors.WaitCursor

                Select Case Me.SQL_PROVIDERComboBox.SelectedItem
                    Case "MS-SQL".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        msg_string = con
                        Dim connection As New SqlClient.SqlConnection(con)
                        connection.Open()
                        Cursor = Cursors.Default


                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                        Else
                            MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                        End If


                        My.Settings.Save()
                    Case "MS-SQLServer".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        msg_string = con
                        Dim connection As New SqlClient.SqlConnection(con)
                        connection.Open()
                        Cursor = Cursors.Default


                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                        Else
                            MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                        End If


                        My.Settings.Save()
                    Case "Oracle"
                        My.Settings.Save()
                        Dim connstr As String
                        Try
                            Dim conn As New OracleConnectionStringBuilder

                            If chkOR_ohne_TNS.Checked Then
                                connstr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" & SERVERTextBox.Text & ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" &
                                    DATENBANKTextBox.Text & ")));User Id=" & USERNAMETextBox.Text & ";Password=" & PASSWORDTextBox.Text & ";"
                            Else
                                conn.DataSource = DATENBANKTextBox.Text
                                conn.UserID = USERNAMETextBox.Text
                                conn.Password = PASSWORDTextBox.Text
                                conn.PersistSecurityInfo = True
                                conn.ConnectionTimeout = 120
                                connstr = conn.ConnectionString
                            End If

                            Dim connection As New OracleConnection(connstr)
                            connection.Open()

                            If USER_LANGUAGE = "de-DE" Then
                                MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                            Else
                                MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                            End If
                            My.Settings.Save()

                        Catch ex As Exception
                            MsgBox(ex.Message & vbNewLine & vbNewLine & connstr, MsgBoxStyle.Critical, "Unexpected error in Verbindungsaufbau Oracle:")
                        End Try

                    Case Else
                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Test zur Zeit nicht möglich", MsgBoxStyle.Information, "Achtung:")
                        Else
                            MsgBox("Test not available", MsgBoxStyle.Information, "Warning:")
                        End If
                End Select
            End If


        Catch ex As Exception
            Dim template As String = ex.Message
            Clipboard.SetDataObject(template)
            MsgBox("Unexpected error inm Verbindungsaufbau: " & vbNewLine & ex.Message & vbNewLine & "Connection-String: " & vbNewLine & msg_string, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub cmbInitialCatalog_Click(sender As Object, e As EventArgs) Handles cmbInitialCatalog.Click
        Get_MSSQL_Databases()
    End Sub
    Sub Get_MSSQL_Databases()
        Try
            ' Dim csb As New SqlClient.SqlConnectionStringBuilder
            If Me.SQL_PROVIDERComboBox.SelectedIndex <> -1 Then

                Select Case Me.SQL_PROVIDERComboBox.SelectedItem
                    Case "MS-SQL".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        'csb.DataSource = Me.txtDataSource.Text
                        'csb.IntegratedSecurity = False
                        'csb.UserID = Me.txtUserId.Text
                        'csb.Password = Me.txtPassword.Text
                        Dim connection As New SqlClient.SqlConnection(con)
                        'während Verbindungsaufbau Sanduhr-Mauszeiger
                        Cursor = Cursors.WaitCursor
                        connection.Open()
                        Cursor = Cursors.Default
                        ' SQLCommand-Objekt über alle Datenbanken erstellen
                        Dim cmd As New SqlClient.SqlCommand("SELECT [name] FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')", connection)
                        'cmd.CommandType = CommandType.
                        ' Ausführen und Ergebnis in einer ListBox anzeigen
                        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
                        If dr.HasRows Then
                            cmbInitialCatalog.Items.Clear()
                            Do While dr.Read
                                cmbInitialCatalog.Items.Add(dr("name"))
                            Loop
                            cmbInitialCatalog.DroppedDown = True
                        End If
                    Case "MS-SQLServer".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        'csb.DataSource = Me.txtDataSource.Text
                        'csb.IntegratedSecurity = False
                        'csb.UserID = Me.txtUserId.Text
                        'csb.Password = Me.txtPassword.Text
                        Dim connection As New SqlClient.SqlConnection(con)
                        'während Verbindungsaufbau Sanduhr-Mauszeiger
                        Cursor = Cursors.WaitCursor
                        connection.Open()
                        Cursor = Cursors.Default
                        ' SQLCommand-Objekt über alle Datenbanken erstellen
                        Dim cmd As New SqlClient.SqlCommand("SELECT [name] FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')", connection)
                        'cmd.CommandType = CommandType.
                        ' Ausführen und Ergebnis in einer ListBox anzeigen
                        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
                        If dr.HasRows Then
                            cmbInitialCatalog.Items.Clear()
                            Do While dr.Read
                                cmbInitialCatalog.Items.Add(dr("name"))
                            Loop
                            cmbInitialCatalog.DroppedDown = True
                        End If
                End Select
            End If


        Catch ex As Exception
            Cursor = Cursors.Default

            Dim template As String = ex.Message
            Clipboard.SetDataObject(template)
            MsgBox("Unexpected error inm Verbindungsaufbau: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub TBDD_CONNECTIONBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_CONNECTIONBindingSource.AddingNew
        Me.MyDataset.TBDD_CONNECTION.ERSTELLTWERColumn.DefaultValue = Environment.UserName
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs)
        Load_Conns()
    End Sub

    Private Sub TBDD_CONNECTIONBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_CONNECTIONBindingSource.CurrentChanged
        Me.labelStatus.Caption = String.Empty
    End Sub

    Private Sub BEMERKUNGTextBox_TextChanged(sender As Object, e As EventArgs) Handles BEMERKUNGTextBox.TextChanged
        If BEMERKUNGTextBox.Text.Contains("without tnsnames") Then
            Me.chkOR_ohne_TNS.Checked = True
        Else
            Me.chkOR_ohne_TNS.Checked = False
        End If
    End Sub

    Private Sub frmConnections_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Load_Conns()
        Threading.Thread.Sleep(300)
        Load_Conns()
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        TBDD_CONNECTIONBindingSource.AddNew()
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Load_Conns()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Try
            Me.Validate()
            Me.TBDD_CONNECTIONBindingSource.EndEdit()
            If Me.MyDataset.TBDD_CONNECTION.GetChanges Is Nothing = False Then
                Me.GEANDERTWERTextBox.Text = Environment.UserName
                Me.TBDD_CONNECTIONBindingSource.EndEdit()
                TBDD_CONNECTIONTableAdapter.Update(MyDataset.TBDD_CONNECTION)
                Me.labelStatus.Caption = "Verbindung gespeichert!"
            Else
                Me.labelStatus.Caption = String.Empty
            End If
        Catch ex As Exception
            MsgBox("Error in Save Connection:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        'Catch ex As Exception

        '    Dim oConnectionGuid As Integer
        '    oConnectionGuid = CurrentGuid

        '    ' StringBuilder VORHER deklarieren!
        '    Dim beingUsedAt As New Text.StringBuilder()

        '    If oConnectionGuid <> 0 Then
        '        Dim oSql As String =
        '    "SELECT DA.BEZEICHNUNG AS Profil,
        '            IM.NAME        AS IndexName,
        '            'Manuell'      AS IndexTyp
        '     FROM TBDD_DOKUMENTART DA (NOLOCK)
        '     INNER JOIN TBDD_INDEX_MAN IM 
        '         ON DA.GUID = IM.DOK_ID
        '     WHERE IM.CONNECTION_ID = @connectionGuid

        '     UNION ALL

        '     SELECT DA.BEZEICHNUNG AS Profil,
        '            IA.INDEXNAME   AS IndexName,
        '            'Automatisch'  AS IndexTyp
        '     FROM TBDD_DOKUMENTART DA (NOLOCK)
        '     INNER JOIN TBDD_INDEX_AUTOM IA 
        '         ON DA.GUID = IA.DOCTYPE_ID
        '     WHERE IA.CONNECTION_ID = @connectionGuid;"

        '        Dim oDataTable As New DataTable()
        '        oSql = oSql.Replace("@connectionGuid", oConnectionGuid.ToString)
        '        oDataTable = DATABASE_ECM.GetDatatable(oSql)

        '        For Each oRow As DataRow In oDataTable.Rows
        '            beingUsedAt.AppendLine($"- Profil: {oRow("Profil")} | {oRow("IndexTyp")}er Index: {oRow("IndexName")}")
        '        Next
        '    End If

        '    Dim msg As String = "Fehler beim Löschen der SQL-Verbindung:" & vbNewLine &
        '            "Die SQL-Verbindung ist noch in Verwendung."

        '    If beingUsedAt.Length > 0 Then
        '        msg &= vbNewLine & vbNewLine & "Verwendet in:" & vbNewLine & beingUsedAt.ToString()
        '    End If

        '    MsgBox(msg, MsgBoxStyle.Critical)
        'End Try
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick

        Dim oCurrentRow As DataRowView = TryCast(TBDD_CONNECTIONBindingSource.Current, DataRowView)
        Dim oConnectionGuid As Integer
        If oCurrentRow IsNot Nothing AndAlso oCurrentRow("GUID") IsNot DBNull.Value Then
            oConnectionGuid = oCurrentRow("GUID")
        End If

        ' StringBuilder VORHER deklarieren!
        Dim oBeingUsedAt As New Text.StringBuilder()
        Dim oAppJobExist As Integer = 0

        If oConnectionGuid <> 0 Then
            Dim oSql As String = "
                SELECT DA.BEZEICHNUNG AS Profil,
                IM.NAME AS IndexName,
                'Manuell' AS IndexTyp
                FROM TBDD_DOKUMENTART DA (NOLOCK)
                INNER JOIN TBDD_INDEX_MAN IM 
                ON DA.GUID = IM.DOK_ID
                WHERE IM.CONNECTION_ID = @connectionGuid

                UNION ALL

                SELECT DA.BEZEICHNUNG AS Profil,
                IA.INDEXNAME AS IndexName,
                'Automatisch' AS IndexTyp
                FROM TBDD_DOKUMENTART DA (NOLOCK)
                INNER JOIN TBDD_INDEX_AUTOM IA 
                ON DA.GUID = IA.DOCTYPE_ID
                WHERE IA.CONNECTION_ID = @connectionGuid;"

            Dim oDataTable As New DataTable()
            oSql = oSql.Replace("@connectionGuid", oConnectionGuid.ToString)
            oDataTable = DATABASE_ECM.GetDatatable(oSql)

            For Each oRow As DataRow In oDataTable.Rows
                oBeingUsedAt.AppendLine($"- Profil: {oRow("Profil")} | {oRow("IndexTyp")}er Index: {oRow("IndexName")}")
            Next

            Dim oSql2 As String = "
                IF (EXISTS (SELECT * FROM [INFORMATION_SCHEMA].[TABLES] (NOLOCK) WHERE [TABLE_NAME] = 'TBAPPSERV_CRON_DETAIL')) BEGIN
	            SELECT Count(CD.DT_NAME)
				FROM TBAPPSERV_CRON_DETAIL AS CD
				INNER JOIN TBDD_CONNECTION AS CO
				ON CD.CON_ID = CO.GUID
				WHERE CON_ID = @connectionGuid
                END; ELSE BEGIN
	            SELECT 0
                END;"

            oSql2 = oSql2.Replace("@connectionGuid", oConnectionGuid.ToString)
            oAppJobExist = DATABASE_ECM.GetScalarValue(oSql2)

        End If

        Dim oMsg As String = "SQL-Verbindung kann nicht gelöscht werden:" & vbNewLine & vbNewLine &
                    "Die SQL-Verbindung ist noch in Verwendung." & vbNewLine

        If oBeingUsedAt.Length = 0 AndAlso oAppJobExist = 0 Then
            TBDD_CONNECTIONBindingSource.RemoveCurrent()
            Exit Sub
        End If

        If oBeingUsedAt.Length > 0 Then
            oMsg &= vbNewLine & vbNewLine & "Verwendet in:" & vbNewLine & oBeingUsedAt.ToString()
        End If

        If oAppJobExist > 0 Then
            oMsg &= vbNewLine & vbNewLine & "Verwendet in:" & vbNewLine & oAppJobExist.ToString() & " Cron Details"
        End If

        MsgBox(oMsg, MsgBoxStyle.Critical)

    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        Dim msg_string As String
        Try
            If Me.SQL_PROVIDERComboBox.SelectedIndex <> -1 Then
                Cursor = Cursors.WaitCursor

                Select Case Me.SQL_PROVIDERComboBox.SelectedItem
                    Case "MS-SQL".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        msg_string = con
                        Dim connection As New SqlClient.SqlConnection(con)
                        connection.Open()
                        Cursor = Cursors.Default


                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                        Else
                            MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                        End If


                        My.Settings.Save()
                    Case "MS-SQLServer".ToUpper
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Data Source=" & Me.SERVERTextBox.Text & ";Initial Catalog=" & Me.cmbInitialCatalog.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbInitialCatalog.Text & ";User Id=" & Me.USERNAMETextBox.Text & ";Password=" & Me.PASSWORDTextBox.Text & ";"
                        End If
                        msg_string = con
                        Dim connection As New SqlClient.SqlConnection(con)
                        connection.Open()
                        Cursor = Cursors.Default


                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                        Else
                            MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                        End If


                        My.Settings.Save()
                    Case "Oracle"
                        My.Settings.Save()
                        Dim connstr As String
                        Try
                            Dim conn As New OracleConnectionStringBuilder

                            If chkOR_ohne_TNS.Checked Then
                                connstr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" & SERVERTextBox.Text & ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" &
                                    DATENBANKTextBox.Text & ")));User Id=" & USERNAMETextBox.Text & ";Password=" & PASSWORDTextBox.Text & ";"
                            Else
                                conn.DataSource = DATENBANKTextBox.Text
                                conn.UserID = USERNAMETextBox.Text
                                conn.Password = PASSWORDTextBox.Text
                                conn.PersistSecurityInfo = True
                                conn.ConnectionTimeout = 120
                                connstr = conn.ConnectionString
                            End If

                            Dim connection As New OracleConnection(connstr)
                            connection.Open()

                            If USER_LANGUAGE = "de-DE" Then
                                MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                            Else
                                MsgBox("Connection to Server successfully established!", MsgBoxStyle.Information, "Success:")
                            End If
                            My.Settings.Save()

                        Catch ex As Exception
                            MsgBox(ex.Message & vbNewLine & vbNewLine & connstr, MsgBoxStyle.Critical, "Unexpected error in Verbindungsaufbau Oracle:")
                        End Try

                    Case Else
                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Test zur Zeit nicht möglich", MsgBoxStyle.Information, "Achtung:")
                        Else
                            MsgBox("Test not available", MsgBoxStyle.Information, "Warning:")
                        End If
                End Select
            End If


        Catch ex As Exception
            Dim template As String = ex.Message
            Clipboard.SetDataObject(template)
            MsgBox("Unexpected error inm Verbindungsaufbau: " & vbNewLine & ex.Message & vbNewLine & "Connection-String: " & vbNewLine & msg_string, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub
End Class