Public Class frmConnection
    Private Sub BtnConnect_Click(sender As System.Object, e As System.EventArgs) Handles BtnConnect.Click
        Try
            Dim con As String = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbDatenbank.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPasswort.Text & ";"
            Dim connection As New SqlClient.SqlConnection(con)
            'während Verbindungsaufbau Sanduhr-Mauszeiger
            Cursor = Cursors.WaitCursor
            connection.Open()
            Cursor = Cursors.Default
            'DialogResult = Windows.Forms.DialogResult.OK
            Dim result As MsgBoxResult
            result = MessageBox.Show("Die Verbindung wurde erfolgreich aufgebaut!" & vbNewLine & "Möchten Sie diese Verbindung nun in der Anwendung speichern?", "Erfolgsmeldung:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = MsgBoxResult.Yes Then
                'Set the construction string
                My.Settings.ConfigConnectionString = con 'csb.ConnectionString
                con = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbMercatorDB.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPasswort.Text & ";"
                'Set the construction string
                My.Settings.ConfigConnStringMercator = con

                My.Settings.Save()
                Me.Close()
            End If

        Catch ex As Exception
            Cursor = Cursors.Default
            MsgBox("Fehler beim Verbindungsaufbau: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub cmbDatenbank_Click(sender As System.Object, e As System.EventArgs) Handles cmbDatenbank.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim csb As New SqlClient.SqlConnectionStringBuilder
            csb.DataSource = Me.txtServer.Text
            csb.IntegratedSecurity = False
            csb.UserID = Me.txtUser.Text
            csb.Password = Me.txtPasswort.Text

            Dim con As String = "Server=" & Me.txtServer.Text & "; User Id=" & Me.txtUser.Text & "; Password=" & Me.txtPasswort.Text & ";"
            Dim connection As New SqlClient.SqlConnection(con) 'csb.ConnectionString)
            connection.Open()
            Dim cmd As New SqlClient.SqlCommand("sp_databases", connection)
            cmd.CommandType = CommandType.StoredProcedure
            ' Ausführen und Ergebnis in einer ListBox anzeigen
            Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                Do While dr.Read
                    cmbDatenbank.Items.Add(dr("Database_Name"))
                Loop
                cmbDatenbank.DroppedDown = True
            End If
            connection.Close()
        Catch ex As Exception
            MsgBox("Fehler bei Datenbank-Connect:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub cmbMercatorDB_Click(sender As System.Object, e As System.EventArgs) Handles cmbMercatorDB.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim csb As New SqlClient.SqlConnectionStringBuilder
            csb.DataSource = Me.txtServer.Text
            csb.IntegratedSecurity = False
            csb.UserID = Me.txtUser.Text
            csb.Password = Me.txtPasswort.Text

            Dim con As String = "Server=" & Me.txtServer.Text & "; User Id=" & Me.txtUser.Text & "; Password=" & Me.txtPasswort.Text & ";"
            Dim connection As New SqlClient.SqlConnection(con) 'csb.ConnectionString)
            connection.Open()
            Dim cmd As New SqlClient.SqlCommand("sp_databases", connection)
            cmd.CommandType = CommandType.StoredProcedure
            ' Ausführen und Ergebnis in einer ListBox anzeigen
            Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                Do While dr.Read
                    cmbMercatorDB.Items.Add(dr("Database_Name"))
                Loop
                cmbMercatorDB.DroppedDown = True
            End If
            connection.Close()
        Catch ex As Exception
            MsgBox("Fehler bei Datenbank-Connect:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub
End Class