Npgsql - 如何传递表?

时间:2018-03-14 16:16:20

标签: vb.net postgresql npgsql

我试图传递一张桌子。在数据库方面,我使用复合类型" type_detalle_ac",并且在vb方面我尝试使用MapCompositeGlobally发送列表,这将是vb中的代码:

Sub ADMINISTRAR_ARTEFACTO(ByVal P As E_ARTEFACTO, ByVal LD As List(Of E_DETALLE_AC))
NpgsqlConnection.MapCompositeGlobally(Of List(Of E_DETALLE_AC))("type_detalle_ac")
Dim CONECTION As New NpgsqlConnection


Try
    CONECTION.ConnectionString = "Host=localhost;Username=postgres;Password=123;Database=ALURHE_DB"
    Dim COMMAND As New NpgsqlCommand
    With COMMAND
        .Connection = CONECTION
        .CommandType = CommandType.StoredProcedure
        .CommandText = "SP_ADM_ARTEFACTO"

        .Parameters.AddWithValue("v_serie", P.P_SERIE)
        .Parameters.AddWithValue("v_tipo_artefacto", P.P_TIPO_ARTEFACTO)
        .Parameters.AddWithValue("v_modelo", P.P_MODELO)
        .Parameters.AddWithValue("v_marca", P.P_MARCA)

        .Parameters.AddWithValue("v_detalle", LD)
        .Parameters.AddWithValue("v_usuario", P.P_USUARIO)
        .Parameters.AddWithValue("v_id_artefacto", If(P.P_TIPO_OPERACION = "M", P.P_ID_ARTEFACTO, DBNull.Value))
        .Parameters.AddWithValue("v_tipo_operacion", P.P_TIPO_OPERACION)

        End With


    CONECTION.Open()

    COMMAND.ExecuteNonQuery()
    CONECTION.Close()

Catch ex As Exception
    If (CONECTION.State = ConnectionState.Open) Then
        CONECTION.Close()
    End If

    Throw ex
End Try
End Sub

这是该类的代码:

Public Class E_DETALLE_AC
   Public Property id_componente As Integer

   Public Property precio As Decimal
   Public Property cantidad As Integer
   Public Property sub_total As Decimal
End Class

但是我收到以下错误:

  

PostgreSQL复合类型public.type_detalle_ac包含字段id_componente,它无法匹配任何CLR类型List`1

我可以做什么来寄表?非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我发现以下内容并且有效,这个链接:

https://gist.github.com/jakejscott/eee8a257fceabbec537c92a8604a516e

在MapCompositeGlobally的一部分中,我只需要放入类而不是列表。该列表将把它放在" v_detalle"参数简单。

Sub ADMINISTRAR_ARTEFACTO(ByVal P As E_ARTEFACTO, ByVal LD As List(Of E_DETALLE_AC))
    NpgsqlConnection.MapCompositeGlobally(Of E_DETALLE_AC)("type_detalle_ac")
    Dim CONECTION As New NpgsqlConnection


    Try
        CONECTION.ConnectionString = "Host=localhost;Username=postgres;Password=123;Database=ALURHE_DB"
        Dim COMMAND As New NpgsqlCommand
        With COMMAND
            .Connection = CONECTION
            .CommandType = CommandType.StoredProcedure
            .CommandText = "sp_adm_artefacto"

            .Parameters.AddWithValue("v_serie", P.P_SERIE)
            .Parameters.AddWithValue("v_tipo_artefacto", P.P_TIPO_ARTEFACTO)
            .Parameters.AddWithValue("v_modelo", P.P_MODELO)
            .Parameters.AddWithValue("v_marca", P.P_MARCA)

            .Parameters.AddWithValue("v_detalle", LD)
            .Parameters.AddWithValue("v_usuario", P.P_USUARIO)
            .Parameters.AddWithValue("v_id_artefacto", If(P.P_TIPO_OPERACION = "M", P.P_ID_ARTEFACTO, DBNull.Value))
            .Parameters.AddWithValue("v_tipo_operacion", P.P_TIPO_OPERACION)
        End With


        CONECTION.Open()

        COMMAND.ExecuteNonQuery()
        CONECTION.Close()

    Catch ex As Exception
        If (CONECTION.State = ConnectionState.Open) Then
            CONECTION.Close()
        End If

        Throw ex
    End Try
End Sub

在数据库方面,声明类型为type_detalle_ac的v_detalle但是作为数组,我也在select中使用unexst

CREATE OR REPLACE FUNCTION public.sp_adm_artefacto(
v_serie character varying DEFAULT NULL::character varying,
v_tipo_artefacto integer DEFAULT NULL::integer,
v_modelo character varying DEFAULT NULL::character varying,
v_marca integer DEFAULT NULL::integer,
v_detalle type_detalle_ac[] DEFAULT NULL::type_detalle_ac[],
v_usuario integer DEFAULT NULL::integer,
v_id_artefacto integer DEFAULT NULL::integer,
v_tipo_operacion character DEFAULT NULL::bpchar)
RETURNS void
LANGUAGE 'plpgsql'

COST 100
VOLATILE 

AS $BODY$

DECLARE 

    VL_ID_ARTEFACTO INTEGER;

BEGIN
    IF V_TIPO_OPERACION = 'I' THEN

        SELECT COALESCE(MAX(ID_ARTEFACTO),0) INTO VL_ID_ARTEFACTO FROM ARTEFACTO;
        VL_ID_ARTEFACTO:=VL_ID_ARTEFACTO+1;

        INSERT INTO ARTEFACTO (ID_ARTEFACTO,SERIE,TIPO_ARTEFACTO,MODELO,MARCA,USUARIO_CREACION,FECHA_CREACION)
        VALUES (VL_ID_ARTEFACTO,v_serie,v_tipo_artefacto,v_modelo,v_marca,V_USUARIO,NOW());

        INSERT INTO DETALLE_AC(ID_ARTEFACTO,ID_COMPONENTE,PRECIO,CANTIDAD,SUB_TOTAL,
                               USUARIO_CREACION,FECHA_CREACION)
        SELECT VL_ID_ARTEFACTO,ID_COMPONENTE,PRECIO,CANTIDAD,SUB_TOTAL,v_usuario,NOW()
        FROM unnest(V_DETALLE);

    END IF;

END;


$BODY$;