存储过程Postgresql和C#WPF

时间:2011-06-22 02:13:51

标签: c# postgresql

我想通过存储过程访问postgres ..当我执行函数时它很成功但是当我使用C#运行它时它有一个错误

  

:或“”“sqlerror附近的语法错误   :select * from   laporan_kab('32 ':: VARCHAR,'14' :: VARCHAR)   如()

我的功能是:

CREATE OR REPLACE FUNCTION laporan_kab(aprop character varying, atahun character varying)
  RETURNS TABLE (kode_wilda varchar(255), wilayah varchar(255), dda integer, statda integer, lainnya integer) AS
$BODY$

DECLARE
    jml_wilda integer;
    jml_dda integer;
    jml_statda integer;
    jml_lainnya integer;
    nm_wilayah varchar(255);
    i integer;
    aprod varchar(255);

BEGIN

    CREATE TEMP TABLE lap (
    akode_wilda varchar(255),
    awilayah varchar(255),
    adda integer,
    astatda integer,
    alainnya integer
    );

    CREATE TEMP TABLE wilda (
    id serial,
    kd_prop varchar(255),
    kd_kab varchar(255),
    kd_kec varchar(255),
    no_urut integer,
    nm_wilda varchar(255)
    );

    INSERT into wilda(kd_prop,kd_kab,kd_kec,no_urut,nm_wilda) SELECT kd_prop,kd_kab,kd_kec,no_urut,nm_wilda from t_history_wilda where is_active = '1' and kd_prop = aprop and kd_kec = '000' and kd_kab != '00';

    SELECT count(*) INTO jml_wilda from wilda;

    i := 1;
    while(i <= jml_wilda)
    LOOP

        SELECT kd_prop||kd_kab||kd_kec||no_urut INTO aprod from wilda WHERE id = i;

        SELECT count(*) INTO jml_dda from t_publikasi where kd_produsen = aprod and kd_bahan_pustaka ='121' and thn_terbit = atahun;
        SELECT count(*) INTO jml_statda from t_publikasi where kd_produsen = aprod and kd_bahan_pustaka ='122' and thn_terbit = atahun;
        SELECT count(*) INTO jml_lainnya from t_publikasi where kd_produsen = aprod and kd_bahan_pustaka !='121' and kd_bahan_pustaka !='122' and thn_terbit = atahun;
        SELECT nm_wilda INTO nm_wilayah from wilda WHERE id = i;

        INSERT INTO lap values (aprod,nm_wilayah,jml_dda,jml_statda,jml_lainnya);
        i := i + 1;
    END LOOP;

    PERFORM * from lap;
    RETURN QUERY SELECT * from lap ;

END 
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION laporan_kab(character varying, character varying) OWNER TO postgres;

我在c#中的代码:

public DataSet LapPubDa(string kode, string tahun)
    {
        DBConnection odbCon = new DBConnection();
        DBData odbData = new DBData();
        DataSet ds = new DataSet();
        DBParameter akode = new DBParameter();
        string aprop = "aprop";
        akode.Name = aprop;
        akode.Type = NpgsqlTypes.NpgsqlDbType.Varchar;
        akode.Value = kode;
        DBParameter atahun = new DBParameter();
        string btahun = "atahun";
        atahun.Name = btahun;
        atahun.Type = NpgsqlTypes.NpgsqlDbType.Varchar;
        atahun.Value = tahun;
        DBParameter[] param = { akode, atahun };
        string cmd = "laporan_kab";
        odbData = odbCon.ExecuteStoredProcedureCommand(cmd, param);
        return ds;
    }

1 个答案:

答案 0 :(得分:1)

可能是因为:

select * from laporan_kab('32'::varchar,'14'::varchar) as() 
-- note the missing "as ('x', 'y', 'z')"

我看到你的查询返回了一些内容:

RETURN QUERY SELECT * from lap ;

所以尝试添加一些输出参数:

NpgsqlParameter firstColumn = new NpgsqlParameter("firstcolumn", NpgsqlDbType.Integer);
firstColumn.Direction = ParameterDirection.Output;
command.Parameters.Add(firstColumn);

请查看此处的文档以获取更多示例:http://npgsql.projects.postgresql.org/docs/manual/UserManual.html,“在查询中使用输出参数”部分。