Imports System
Imports System.IO
Imports System.Collections

Public Class frmSQL_Admin

    Private Sub btnrunSQL_Click(sender As Object, e As EventArgs) Handles btnrunSQL.Click
        If txtSQL.Text <> "" Then
            If txtSQL.Text.ToLower.StartsWith("select") Then
                Dim dt As DataTable = DatabaseFallback.GetDatatableECM(txtSQL.Text) ', "btnrunSQL_Click")
                If Not dt Is Nothing Then
                    XtraTabControl1.SelectedTabPageIndex = 1
                    BindingSource1.DataSource = dt
                End If
            Else
                Dim result As MsgBoxResult = MsgBox("Bitte bestätigen Sie das Ausführen des SQL-Befehls?", MsgBoxStyle.YesNo, "Bestätigung erforderlich:")
                ' wenn Speichern ja
                If result = MsgBoxResult.Yes Then
                    Try
                        Me.Cursor = Cursors.WaitCursor

                        Dim connection As New SqlClient.SqlConnection(CONNECTION_STRING_ECM) 'csb.ConnectionString)
                        connection.Open()
                        Dim cmd As New SqlClient.SqlCommand(txtSQL.Text, connection)
                        cmd.ExecuteNonQuery()
                        MsgBox("Der SQL-Befehl wurde erfolgreich ausgeführt!", MsgBoxStyle.Information, "Erfolgsmeldung:")
                        connection.Close()
                    Catch ex As Exception
                        LOGGER.Error(ex)
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei Datenbank-Connect:")
                    End Try
                    Cursor = Cursors.Default
                End If
            End If
        End If



    End Sub
    Sub Load_ConString(constr As String)
        Dim csb As New SqlClient.SqlConnectionStringBuilder
        csb.ConnectionString = CONNECTION_STRING_ECM
        If csb.Password.Length > 0 Then
            constr = constr.Replace(csb.Password, "XXXXX")
        End If

        Me.TextBox1.Text = constr
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            With OpenFileDialog1
                ' Do

                .Title = "Wählen Sie bitte die SQL-File:"
                If .ShowDialog() = DialogResult.OK Then
                    Dim FILE_NAME As String = .FileName
                    Dim objReader As New System.IO.StreamReader(FILE_NAME)
                    txtSQL.Text = objReader.ReadToEnd
                    objReader.Close()
                End If
            End With

        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei open-File:")
        End Try
      
    End Sub

    Private Sub frmSQL_Admin_Load(sender As Object, e As EventArgs) Handles Me.Load
        Load_ConString(CONNECTION_STRING_ECM)
    End Sub

    Private Sub txtSQL_KeyPress_1(sender As Object, e As KeyPressEventArgs) Handles txtSQL.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True
        End If
    End Sub
End Class