我的Npgsql版本3.2.5-Postgresql 9.6
我用CommandType.StoredProcedure得到这个错误(但是CommandType.Text有效):
NPGQL.PASGRESExtRe::42883:函数Cuuler-Sype选择(pEmail=>文本,密码= >文本)不存在

string sql3 = @"customer_select";

NpgsqlConnection pgcon = new NpgsqlConnection(pgconnectionstring);
pgcon.Open();
NpgsqlCommand pgcom = new NpgsqlCommand(sql3, pgcon);
pgcom.CommandType = CommandType.StoredProcedure;
pgcom.Parameters.AddWithValue(":pEmail", "[email protected]");
pgcom.Parameters.AddWithValue(":pPassword", "eikaylie78");
NpgsqlDataReader pgreader = pgcom.ExecuteReader();

while (pgreader.Read()) {
    string name = pgreader.GetString(1);
    string surname = pgreader.GetString(2);
}

这是数据库中的函数:
CREATE OR REPLACE FUNCTION public.customer_select(
    pemail character varying, ppassword character varying)
RETURNS SETOF "CustomerTest"
LANGUAGE 'plpgsql'
COST 100.0
AS $function$
BEGIN
    RETURN QUERY
        SELECT "CustomerTestId", "FirstName", "LastName", "Email", "Password"
        FROM public."CustomerTest"
        WHERE "Email" = pEmail AND "Password" = pPassword;
END;
$function$;
ALTER FUNCTION public.customer_select(character varying, character varying)
OWNER TO postgres;

最佳答案

Npgsql确实支持命名参数,但参数的大小写与函数不匹配,请尝试pemail而不是pEmailppassword而不是pPassword
注意,在Npgsql中使用CommandType.StoredProcedure比使用CommandType.Text没有特别的优势-两者最终都做了相同的事情。CommandType.StoredProcedure主要是为了简化从SqlServer等的代码移植。

10-05 22:31