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 = DATABASE_ECM.GetDatatable(txtSQL.Text)
                If Not dt Is Nothing Then
                    XtraTabControl1.SelectedTabPageIndex = 1
                    BindingSource1.DataSource = dt
                End If
            Else
                Dim result As MsgBoxResult
                If USER_LANGUAGE = "de-DE" Then
                    result = MsgBox("Bitte bestätigen Sie das Ausführen des SQL-Befehls", MsgBoxStyle.YesNo, "Bestätigung erforderlich:")
                Else
                    result = MsgBox("Please confirm the execution of the SQL-Query", MsgBoxStyle.YesNo, "Confirmation needed:")
                End If

                ' wenn Speichern ja
                If result = MsgBoxResult.Yes Then
                    Try
                        Me.Cursor = Cursors.WaitCursor

                        Dim connection As New SqlClient.SqlConnection(MyConnectionString) 'csb.ConnectionString)
                        connection.Open()
                        Dim cmd As New SqlClient.SqlCommand(txtSQL.Text, connection)
                        cmd.ExecuteNonQuery()

                        If USER_LANGUAGE = "de-DE" Then
                            MsgBox("Der SQL-Befehl wurde erfolgreich ausgeführt!", MsgBoxStyle.Information, "Erfolgsmeldung:")
                        Else
                            MsgBox("SQL-Query successfully executed", MsgBoxStyle.Information, "Success:")
                        End If

                        connection.Close()
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Unexpected error in 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 = MyConnectionString
        constr = constr.Replace(csb.Password, "XXXXX")
        Me.TextBox1.Text = constr
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            With OpenFileDialog1
                ' Do
                If USER_LANGUAGE = "de-DE" Then
                    .Title = "Wählen Sie bitte die SQL-Datei:"
                Else
                    .Title = "Please select the SQL-File:"
                End If

                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
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Unexpected error in open-File:")
        End Try
      
    End Sub

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

    Private Sub txtSQL_KeyPress(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

    Private Sub GridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles GridView1.KeyDown
        If e.Control AndAlso e.KeyCode = Keys.C Then
            If GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.FocusedColumn) IsNot Nothing AndAlso GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.FocusedColumn).ToString() <> String.Empty Then
                Clipboard.SetText(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.FocusedColumn).ToString())
            Else
                If USER_LANGUAGE = "de-DE" Then
                    MsgBox("Der Wert in der ausgewählten Zelle ist Null oder Leer", MsgBoxStyle.Information)
                Else
                    MsgBox("The value in the selected cell is null or empty!", MsgBoxStyle.Information)
                End If
            End If
        End If
    End Sub
End Class