Imports System.Data.SqlClient Imports System.Reflection Public Class cAuditFlow_Pruefungen '=== Properties entsprechend der Tabelle === Public Property wartPruef_Id As Integer Public Property wartPruef_wartPOId As Integer Public Property wartPruef_Faelligkeitsdatum As Date Public Property wartPruef_Erledigung As Boolean = False Public Property wartPruef_Erledigung_Datum As Nullable(Of Date) Public Property wartPruef_Erledigung_BestaetigtMaId As Nullable(Of Integer) Public Property wartPruef_Erledigung_BestaetigtMaName As String Public Property wartPruef_Erledigung_Bemerkung As String Public Property wartPruef_Anhaenge_daId As Nullable(Of Integer) Public Property wartPruef_Anhaenge_Count As Integer Public Property wartPruef_Bemerkung As String Public Property wartPruef_MailErinnerung_Gesendet As Boolean = False Public Property wartPruef_MailErinnerung_GesendetAn As String Public Property wartPruef_MailErinnerung_GesendetAm As Nullable(Of Date) '=== Status / Helper === Public hasEntry As Boolean = False Private ReadOnly SQL As New SQL '=== CTORs === Public Sub New(wartPruef_Id As Integer) Me.wartPruef_Id = wartPruef_Id LOAD() End Sub Public Sub New() ' Für neue Datensätze ohne sofortiges LOAD End Sub '=== Parameterliste === Private Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) ' PK als PrimaryParam list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Id", wartPruef_Id, , True)) ' übrige Spalten list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_wartPOId", wartPruef_wartPOId)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Faelligkeitsdatum", wartPruef_Faelligkeitsdatum)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Erledigung", wartPruef_Erledigung)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Erledigung_Datum", wartPruef_Erledigung_Datum)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Erledigung_BestaetigtMaId", wartPruef_Erledigung_BestaetigtMaId)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Erledigung_BestaetigtMaName", wartPruef_Erledigung_BestaetigtMaName)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Erledigung_Bemerkung", wartPruef_Erledigung_Bemerkung)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Anhaenge_daId", wartPruef_Anhaenge_daId)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Anhaenge_Count", wartPruef_Anhaenge_Count)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_Bemerkung", wartPruef_Bemerkung)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_MailErinnerung_Gesendet", wartPruef_MailErinnerung_Gesendet)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_MailErinnerung_GesendetAn", wartPruef_MailErinnerung_GesendetAn)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wartPruef_MailErinnerung_GesendetAm", wartPruef_MailErinnerung_GesendetAm)) Return list End Function '=== SAVE: Insert/Update in einer Transaktion === Public Function SAVE() As Boolean Try Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() Dim sqlstr As String = "BEGIN TRAN " & "IF EXISTS(SELECT * FROM [tblAuditFlow_Pruefungen] WHERE [wartPruef_Id]=@wartPruef_Id) " & "BEGIN " & getUpdateCmd() & " END " & "ELSE " & "BEGIN " & getInsertCmd() & " END " & "COMMIT TRAN " Return SQL.doSQLVarList(sqlstr, "ADMIN", , list) Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) Return False End Try End Function '=== LOAD: Properties via Reflection aus DataReader === Public Sub LOAD() Try hasEntry = False Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN() Using cmd As New SqlCommand("SELECT * FROM [tblAuditFlow_Pruefungen] WHERE [wartPruef_Id]=@wartPruef_Id", conn) cmd.Parameters.AddWithValue("@wartPruef_Id", wartPruef_Id) Using dr = cmd.ExecuteReader() If dr.Read() Then For Each li In getParameterList() Dim propInfo As PropertyInfo = Me.GetType().GetProperty(li.Scalarvariable) If propInfo Is Nothing Then Continue For 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 End Using End Using End Using Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub '=== UPDATE-Kommando dynamisch === Private Function getUpdateCmd() As String Try Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() Dim setParts As New List(Of String) For Each i In list If Not i.isPrimaryParam Then setParts.Add("[" & i.Text & "]=@" & i.Scalarvariable) End If Next Dim setClause As String = String.Join(",", setParts) Return "UPDATE [tblAuditFlow_Pruefungen] SET " & setClause & " WHERE [wartPruef_Id]=@wartPruef_Id" Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) Return "" End Try End Function '=== INSERT-Kommando dynamisch === Private Function getInsertCmd() As String Try Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() Dim cols As New List(Of String) Dim vals As New List(Of String) For Each i In list If Not i.isPrimaryParam Then cols.Add("[" & i.Text & "]") vals.Add("@" & i.Scalarvariable) End If Next Return "INSERT INTO [tblAuditFlow_Pruefungen] (" & String.Join(",", cols) & ") VALUES (" & String.Join(",", vals) & ")" Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) Return "" End Try End Function Public Shared Function ExistiertBereits(pruefobjektID As Integer, faelligkeit As Date) As Boolean Try Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN() Using cmd As New SqlCommand("SELECT COUNT(*) FROM [tblAuditFlow_Pruefungen] WHERE [wartPruef_wartPOId]=@poID AND [wartPruef_Faelligkeitsdatum]=@datum", conn) cmd.Parameters.AddWithValue("@poID", pruefobjektID) cmd.Parameters.AddWithValue("@datum", faelligkeit.Date) Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar()) Return count > 0 End Using End Using Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) Return False End Try End Function End Class