
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cTextkonserve
    Property txt_Id As Integer
    Property txt_kategorie As String
    Property txt_bezeichnung As String
    Property txt_sprache As String
    Property txt_text As String
    Property txt_betreff As String
    Property txt_FIRMA As String
    Property txt_firmaSig As Integer = 1


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(txt_Id)
        '  If txt_Id IsNot Nothing Then
        Me.txt_Id = txt_Id
        LOAD()
        '  End If
    End Sub

    Sub New(txt_kategorie, txt_bezeichnung, txt_sprache, txt_FIRMA)
        '  If txt_Id IsNot Nothing Then
        Me.txt_kategorie = txt_kategorie
        Me.txt_bezeichnung = txt_bezeichnung
        Me.txt_sprache = txt_sprache
        Me.txt_FIRMA = txt_FIRMA
        LOAD(txt_kategorie, txt_bezeichnung, txt_sprache, txt_FIRMA)
        '  End If
    End Sub



    Public Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_Id", txt_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_kategorie", txt_kategorie))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_bezeichnung", txt_bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_sprache", txt_sprache))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_text", txt_text))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_betreff", txt_betreff))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_FIRMA", txt_FIRMA))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("txt_firmaSig", txt_firmaSig))

        Return list
    End Function


    Public Function SAVE(Optional errHinweis = "") As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblTextkonserve WITH(updlock,serializable) WHERE txt_Id=@txt_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        txt_Id = SQL.doSQLVarListID(txt_Id, sqlstr, "FMZOLL", , list,, errHinweis)
        Return txt_Id > 0
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL
                Using cmd As New SqlCommand("SELECT * FROM tblTextkonserve WHERE txt_Id=@txt_Id  ", conn)
                    cmd.Parameters.AddWithValue("@txt_Id", txt_Id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If
                            hasEntry = True
                        Next

                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Sub LOAD(txt_kategorie, txt_bezeichnung, txt_sprache, txt_FIRMA)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblTextkonserve WHERE txt_kategorie=@txt_kategorie AND txt_bezeichnung=@txt_bezeichnung AND txt_sprache=@txt_sprache  AND txt_FIRMA=@txt_FIRMA   ", conn)
                    cmd.Parameters.AddWithValue("@txt_kategorie", txt_kategorie)
                    cmd.Parameters.AddWithValue("@txt_bezeichnung", txt_bezeichnung)
                    cmd.Parameters.AddWithValue("@txt_sprache", txt_sprache)
                    cmd.Parameters.AddWithValue("@txt_FIRMA", txt_FIRMA)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If
                            hasEntry = True
                        Next

                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

            Dim str As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblTextkonserve] SET " & str & " WHERE txt_Id=@txt_Id ")

        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function


    Public Function getInsertCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "],"
                    values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            values = values.Substring(0, values.Length - 1) 'wg. ','
            Return (" INSERT INTO tblTextkonserve  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function

    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTextkonserve] WITH(updlock,serializable) WHERE txt_Id=" & Me.txt_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Shared Function DELETE(id As Integer) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTextkonserve] WITH(updlock,serializable) WHERE txt_Id=" & id
        Return (New VERAG_PROG_ALLGEMEIN.SQL).doSQL(sqlstr, "FMZOLL")
    End Function

End Class
