Imports Oracle.ManagedDataAccess.Client
Imports System.Data.Odbc


Public Class frmConnections

    Private Sub TBDD_CONNECTIONBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBDD_CONNECTIONBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.TBDD_CONNECTIONBindingSource.EndEdit()
            If DD_ECMAdmin.TBDD_CONNECTION.GetChanges Is Nothing = False Then
                CHANGED_WHOTextBox.Text = Environment.UserName
                TBDD_CONNECTIONBindingSource.EndEdit()
                TBDD_CONNECTIONTableAdapter.Update(DD_ECMAdmin.TBDD_CONNECTION)
                Me.lblSave.Visible = True
            Else
                Me.lblSave.Visible = False
            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 "DD_ECMAdmin.TBDD_CONNECTION". Sie können sie bei Bedarf verschieben oder entfernen.

        'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_ECMDataset.TBDD_CONNECTION". Sie können sie bei Bedarf verschieben oder entfernen.
        Me.TBDD_CONNECTIONTableAdapter.Connection.ConnectionString = MyConnectionString
        Load_Connections()
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Load_Connections()
    End Sub

    Sub Load_Connections()
        Try
            Me.TBDD_CONNECTIONTableAdapter.Fill(Me.DD_ECMAdmin.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
            Me.btnCheck_Con.Visible = True
            CheckBoxwin_aut.Visible = False
            chkOR_ohne_TNS.Visible = False
            Select Case Me.SQL_PROVIDERComboBox.SelectedItem
                Case "MS-SQLServer"
                    cmbDatabase.Items.Clear()
                    ' DATABASETextBox.Visible = False
                    Me.lblServer.Text = "Datenbank-Server (IP-Adresse oder Name)*:"
                    Me.lblInitialCatalog.Text = "Datenbank-Name:"
                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""
                    Me.lblUserId.Text = "UserId:"
                    Me.lblPassword.Text = "Passwort:"
                    Me.lblInitialCatalog.Visible = False
                    cmbDatabase.Items.Add(DATABASETextBox.Text)
                    'Me.DATENBANKTextBox.Visible = False
                    Me.cmbDatabase.Visible = True
                    'Me.lblDatenbank.Visible = True
                    CheckBoxwin_aut.Visible = True
                Case "Oracle"
                    ' DATABASETextBox.Visible = True
                    Me.cmbDatabase.Visible = False
                    Me.lblServer.Text = "IP Oracle-Server:"
                    Me.lblInitialCatalog.Visible = True
                    Me.lblInitialCatalog.Text = "Servicename:"
                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""
                    Me.lblUserId.Text = "UserId:"
                    Me.lblPassword.Text = "Passwort:"


                    'Me.DATENBANKTextBox.Visible = True
                    chkOR_ohne_TNS.Visible = True
                Case "ODBC (SAP - MAXDB)"
                    Me.lblServer.Text = "Servername:"
                    Me.lblInitialCatalog.Text = "Databasename:"
                    Me.lblInitialCatalog.Visible = True
                    Me.cmbDatabase.Visible = False

                    Me.SERVERTextBox.ReadOnly = False
                    Me.SERVERTextBox.BackColor = System.Drawing.SystemColors.Window
                    Me.SERVERTextBox.Text = ""
                    Me.lblUserId.Text = "Username:"
                    Me.lblPassword.Text = "Password:"
                Case "ODBC"
                    Me.lblServer.Text = "DSN Name:"
                    Me.lblInitialCatalog.Visible = False
                    Me.cmbDatabase.Visible = False
                    Me.DATABASETextBox.Visible = False
                    Me.SERVERTextBox.Visible = False
                    Me.lblUserId.Text = "Username:"
                    Me.lblPassword.Text = "Password:"
            End Select
            If Me.SQL_PROVIDERComboBox.SelectedItem <> "ODBC" Then

            End If
            Me.SERVERTextBox.Visible = True
            Me.lblServer.Visible = True
            Me.lblUserId.Visible = True
            Me.lblPassword.Visible = True
            Me.USERNAMETextBox.Visible = True
            Me.PASSWORDTextBox.Visible = True
        Else
            Me.lblServer.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"
            Me.BEMERKUNGTextBox.Enabled = False
        Else
            Me.BEMERKUNGTextBox.Text = ""
            Me.BEMERKUNGTextBox.Enabled = True
        End If
    End Sub

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

                Select Case Me.SQL_PROVIDERComboBox.SelectedItem
                    Case "MS-SQLServer"
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbDatabase.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbDatabase.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
                        MsgBox("Die SQL-Server Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                        My.Settings.Save()
                        'DialogResult = Windows.Forms.DialogResult.OK
                    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=" & _
                                    DATABASETextBox.Text & ")));User Id=" & USERNAMETextBox.Text & ";Password=" & PASSWORDTextBox.Text & ";"
                            Else
                                conn.DataSource = DATABASETextBox.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()

                            MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                            My.Settings.Save()

                        Catch ex As Exception
                            MsgBox(ex.Message & vbNewLine & vbNewLine & connstr, MsgBoxStyle.Critical, "Fehler bei Verbindungsaufbau Oracle:")
                        End Try
                    Case "ODBC (SAP - MAXDB)"
                        Dim Connstring
                        Try
                            Dim conn As New OdbcConnection("driver=sap db;servernode=" & SERVERTextBox.Text & ";serverdb=" + DATABASETextBox.Text & _
                                                    ";uid=" & USERNAMETextBox.Text & ";pwd=" + PASSWORDTextBox.Text)
                            Connstring = conn.ConnectionString
                            conn.Open()
                            MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                            conn.Close()
                        Catch ex As Exception
                            MsgBox(ex.Message & vbNewLine & vbNewLine & Connstring, MsgBoxStyle.Critical, "Error in Create Conenction SAPMAX DB:")
                        End Try
                    Case "ODBC"
                        Dim Connstring
                        Try
                            Dim conn As New OdbcConnection("dsn=" & SERVERTextBox.Text & ";uid=" & USERNAMETextBox.Text & ";pwd=" + PASSWORDTextBox.Text)
                            Connstring = conn.ConnectionString
                            conn.Open()
                            MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, "Erfolg:")
                            conn.Close()
                        Catch ex As Exception
                            MsgBox(ex.Message & vbNewLine & vbNewLine & Connstring, MsgBoxStyle.Critical, "Error in Create Conenction ODBC:")
                        End Try
                    Case Else
                        MsgBox("Test zur Zeit nicht möglich", MsgBoxStyle.Information, "Achtung:")
                End Select
            End If


        Catch ex As Exception
            Dim template As String = ex.Message
            Clipboard.SetDataObject(template)
            MsgBox("Fehler beim 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 cmbDatabase.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-SQLServer"
                        Dim con As String
                        If CheckBoxwin_aut.Checked Then
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbDatabase.Text & ";Trusted_Connection=True;"
                        Else
                            con = "Server=" & Me.SERVERTextBox.Text & ";Database=" & Me.cmbDatabase.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
                            cmbDatabase.Items.Clear()
                            Do While dr.Read
                                cmbDatabase.Items.Add(dr("name"))
                            Loop
                            cmbDatabase.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("Fehler beim Verbindungsaufbau: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    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 TBDD_CONNECTIONBindingSource_AddingNew1(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_CONNECTIONBindingSource.AddingNew
        Me.DD_ECMAdmin.TBDD_CONNECTION.ERSTELLTWERColumn.DefaultValue = Environment.UserName
        Me.DD_ECMAdmin.TBDD_CONNECTION.ERSTELLTWANNColumn.DefaultValue = Date.Now
    End Sub

    Private Sub TBDD_CONNECTIONBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_CONNECTIONBindingSource.CurrentChanged
        Me.lblSave.Visible = False
        Select Case Me.SQL_PROVIDERComboBox.SelectedItem
            Case "MS-SQLServer"
                cmbDatabase.Items.Clear()
                cmbDatabase.Items.Add(DATABASETextBox.Text)
                cmbDatabase.SelectedIndex = 0
        End Select

    End Sub

    Private Sub frmConnections_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Select Case Me.SQL_PROVIDERComboBox.SelectedItem
            Case "MS-SQLServer"
                cmbDatabase.Items.Clear()
                cmbDatabase.Items.Add(DATABASETextBox.Text)
                cmbDatabase.SelectedIndex = 0
        End Select
    End Sub
End Class