

Imports System.Data.SqlClient
Imports System.Reflection

Public Class cERS
    Property ers_id As Integer
    Property ers_progId As Integer
    Property ers_progName As Object = Nothing
    Property ers_progVersion As Object = Nothing
    Property ers_datetime As DateTime = Now
    Property ers_Testsystem As Boolean
    Property ers_Type As Object = Nothing
    Property ers_EMail As Object = Nothing
    Property ers_maId As Integer = -1
    Property ers_userName As Object = Nothing
    Property ers_Firma As Object = Nothing
    Property ers_errCode As Object = Nothing
    Property ers_errStack As Object = Nothing
    Property ers_errMessage As Object = Nothing
    Property ers_errMethodName As Object = Nothing
    Property ers_infotext As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(ers_id)
        Me.ers_id = ers_id
        LOAD()
    End Sub

    Shared Function saveErr(ers_Type, ers_errMessage, ers_errStack, ers_errCode, ers_errMethodName, Optional ers_EMail = Nothing, Optional ers_infotext = Nothing) As Boolean
        Try
            Dim ERS As New cERS
            ERS.ers_progId = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGID
            ERS.ers_userName = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            ERS.ers_maId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
            ERS.ers_Testsystem = VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM
            ERS.ers_progName = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGNAME
            ERS.ers_progVersion = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGVERSION

            ERS.ers_Type = ers_Type
            ERS.ers_EMail = ers_EMail
            ERS.ers_Firma = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            ERS.ers_errMessage = ers_errMessage
            ERS.ers_errStack = ers_errStack
            ERS.ers_errCode = ers_errCode
            ERS.ers_errMethodName = ers_errMethodName
            ERS.ers_infotext = ers_infotext

            Return ERS.SAVE()

        Catch ex As Exception
            'was soll hier noch helfen??
        End Try
    End Function


    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("ers_id", ers_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_progId", ers_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_progVersion", ers_progVersion))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_progName", ers_progName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_datetime", ers_datetime))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_Testsystem", ers_Testsystem))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_Type", ers_Type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_EMail", ers_EMail))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_maId", ers_maId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_userName", ers_userName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_Firma", ers_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_errCode", ers_errCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_errStack", ers_errStack))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_errMessage", ers_errMessage))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_errMethodName", ers_errMethodName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ers_infotext", ers_infotext))
        Return list
    End Function



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

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblERS  WHERE ers_id=@ers_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "ADMIN", False, list)
    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblERS WHERE ers_id=@ers_id  ", conn)
                    cmd.Parameters.AddWithValue("@ers_id", ers_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

                        Next
                        hasEntry = True
                    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 [tblERS] SET " & str & " WHERE ers_id=@ers_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 tblERS  (" & 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

End Class
