martes, 24 de diciembre de 2019

crud vista

 Dim nombre_vista As String = "vista_estado_sii"
        Dim query_vista_existe As String = "SELECT 1 FROM sys.views WHERE name=" & "'" & nombre_vista & "'"
        Dim query_vista_crear As String = "CREATE VIEW " & nombre_vista & " AS " & My.Resources.ejemplo_vista
        Dim vista_existe As Integer = get_vista_crud(conex, query_vista_existe)


        If vista_existe = 1 Then ' Vista existe; eliminar y crear
            Dim query_vista_eliminar As String = "DROP VIEW " & nombre_vista
            Dim eliminar_vista As Integer = get_vista_crud(conex, query_vista_eliminar)

            If eliminar_vista = 0 Then
                Dim crear_vista As Integer = get_vista_crud(conex, query_vista_crear)
                If crear_vista = 0 Then
                    MessageBox.Show("La vista se ha creado correctamente", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If

        Else ' Vista no existe; crear

            Dim crear_vista As Integer = get_vista_crud(conex, query_vista_crear)
            If crear_vista = 0 Then
                MessageBox.Show("La vista se ha creado correctamente", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        End If

        '1 Preguntar si existe
        '2 Si existe, borrar y crear vista
        '3 Si no existe, crear vista


Public Function get_vista_crud(ByVal conex As String, ByVal query As String) As Integer
        Dim return_value As Integer = 0
        Dim con As New SqlConnection(conex)
        con.Open()
        Using cmd As New SqlCommand(query, con)
            return_value = cmd.ExecuteScalar()
        End Using
        con.Close()
        Return return_value
    End Function

No hay comentarios.:

Publicar un comentario