执行带有参数postgresql的存储过程
Execute stored procedure with parameters postgresql
我的 Npgsql 版本 3.2.5 - Postgresql 9.6
我在使用 CommandType.StoredProcedure
时遇到此错误(但 CommandType.Text
有效):
Npgsql.PostgresException: '42883: function customer_select(pEmail => text, Password => text) does not exist'
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", "myemail@hotmail.com");
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;
根据 issue,NPS SQL 不支持命名参数,因为 PostgreSQL 不支持。但您可以尝试以下操作:
string sql3 = @"select * from customer_select(:pEmail, :pPassword)";
NpgsqlConnection pgcon = new NpgsqlConnection(pgconnectionstring);
pgcon.Open();
NpgsqlCommand pgcom = new NpgsqlCommand(sql3, pgcon);
pgcom.CommandType = CommandType.Text;
pgcom.Parameters.AddWithValue("pEmail", "myemail@hotmail.com");
pgcom.Parameters.AddWithValue("pPassword", "eikaylie78");
NpgsqlDataReader pgreader = pgcom.ExecuteReader();
while (pgreader.Read()) {
string name = pgreader.GetString(1);
string surname = pgreader.GetString(2);
}
据此issue
Npgsql 确实支持命名参数,但您的参数大小写与您的函数不匹配,尝试 pemail
而不是 pEmail
和 ppassword
而不是 pPassword
.
请注意,在 Npgsql 中使用 CommandType.StoredProcedure
与 CommandType.Text
相比并没有特别的优势 - 两者最终都会做同样的事情。 CommandType.StoredProcedure
主要是方便从SqlServer等移植代码
我的 Npgsql 版本 3.2.5 - Postgresql 9.6
我在使用 CommandType.StoredProcedure
时遇到此错误(但 CommandType.Text
有效):
Npgsql.PostgresException: '42883: function customer_select(pEmail => text, Password => text) does not exist'
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", "myemail@hotmail.com");
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;
根据 issue,NPS SQL 不支持命名参数,因为 PostgreSQL 不支持。但您可以尝试以下操作:
string sql3 = @"select * from customer_select(:pEmail, :pPassword)";
NpgsqlConnection pgcon = new NpgsqlConnection(pgconnectionstring);
pgcon.Open();
NpgsqlCommand pgcom = new NpgsqlCommand(sql3, pgcon);
pgcom.CommandType = CommandType.Text;
pgcom.Parameters.AddWithValue("pEmail", "myemail@hotmail.com");
pgcom.Parameters.AddWithValue("pPassword", "eikaylie78");
NpgsqlDataReader pgreader = pgcom.ExecuteReader();
while (pgreader.Read()) {
string name = pgreader.GetString(1);
string surname = pgreader.GetString(2);
}
据此issue
Npgsql 确实支持命名参数,但您的参数大小写与您的函数不匹配,尝试 pemail
而不是 pEmail
和 ppassword
而不是 pPassword
.
请注意,在 Npgsql 中使用 CommandType.StoredProcedure
与 CommandType.Text
相比并没有特别的优势 - 两者最终都会做同样的事情。 CommandType.StoredProcedure
主要是方便从SqlServer等移植代码