Imports System.Data.SqlClient
Imports System.Reflection

Public Class cDEBundeslaenderPLZ

    Property PLZ As Integer
    Property Bundesland As String = ""
    Property Kreis As String = ""
    Property Typ As String = ""
    Property Code As String = ""

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(PLZ)
        Me.PLZ = PLZ
        LOADByPLZ(PLZ)
    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("PLZ", PLZ))     '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bundesland", Bundesland)) '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kreis", Kreis))  '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Typ", Typ))  '    INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Code", Code))       '          VARCHAR(200) Not NULL, 
        Return list
    End Function



    Public Shared Function LOADByPLZ(PLZ As Integer) As cDEBundeslaenderPLZ
        Try
            Dim VK As New cDEBundeslaenderPLZ
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDEBundeslaenderPLZ WHERE PLZ=@PLZ  ", conn)
                    cmd.Parameters.AddWithValue("@PLZ", PLZ)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In VK.getParameterList()
                            Dim propInfo As PropertyInfo = VK.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(VK, Nothing)
                            Else
                                propInfo.SetValue(VK, dr.Item(li.Text))
                            End If
                        Next
                        dr.Close()
                        Return VK
                    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
        Return Nothing
    End Function



    Public Shared Function LOADBundeslandCode_ByPLZ(PLZ As Integer) As String
        Try
            Dim VK As New cDEBundeslaenderPLZ
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT Code FROM tblDEBundeslaenderPLZ WHERE PLZ=@PLZ  ", conn)
                    cmd.Parameters.AddWithValue("@PLZ", PLZ)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Return dr.Item("Code")
                    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
        Return ""
    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 tblDEBundeslaenderPLZ WITH(updlock,serializable) WHERE post_id=@post_id) " &
    '                   " BEGIN " & getUpdateCmd() & " END " &
    '                   " Else " &
    '                   " BEGIN " & getInsertCmd() & " END " &
    '                   " commit tran "

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



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

    '                        If dr.Item(l.Text) Is DBNull.Value Then
    '                            propInfo.SetValue(Me, Nothing)
    '                        Else
    '                            propInfo.SetValue(Me, dr.Item(l.Text))
    '                        End If

    '                    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 [tblDEBundeslaenderPLZ] SET " & str & " WHERE post_id=@post_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 tblDEBundeslaenderPLZ  (" & 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 [tblDEBundeslaenderPLZ] WITH(updlock,serializable) WHERE post_id=" & Me.post_id
    '    Return SQL.doSQL(sqlstr, "FMZOLL")
    'End Function


End Class
