C# 使用参数postgresql执行存储过程
我的Npgsql版本3.2.5-Postgresql 9.6 我通过C# 使用参数postgresql执行存储过程,c#,postgresql,npgsql,stored-functions,C#,Postgresql,Npgsql,Stored Functions,我的Npgsql版本3.2.5-Postgresql 9.6 我通过CommandType.storedProcess获得此错误(但CommandType.Text有效): Npgsql.postgresception:'42883:函数customer_select(pEmail=>text,Password=>text)不存在' 这是数据库中的函数: CREATE OR REPLACE FUNCTION public.customer_select( pemail character
CommandType.storedProcess
获得此错误(但CommandType.Text
有效):
Npgsql.postgresception:'42883:函数customer_select(pEmail=>text,Password=>text)不存在'
这是数据库中的函数:
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;
根据协议,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);
}
根据这一点,Npgsql确实支持命名参数,但您的参数的大小写与您的函数不匹配,请尝试使用
pemail
而不是pemail
和ppassword
而不是ppassword
请注意,与Npgsql一起使用
CommandType.storedProcess
相比,使用CommandType.Text
没有什么特别的优势-两者最终都会做相同的事情CommandType.StoredProcess
主要用于简化从SqlServer等移植代码的过程。提供详细的错误消息全文:函数customer_select(pEmail=>text,pPassword=>text)不存在Npgsql.PostgresException发生HResult=0x80004005 message=42883:函数customer_select(pEmail=>text,pPassword=>text)不存在Source=Npgsql StackTrace:at Npgsql.NpgsqlConnector.d_u148.MoveNext()atpgcom.CommandType=CommandType.text;它可以使用此代码我修复了参数名称(pEmail到pEmail),当我使用“CommandType.StoredProcedure”时它可以工作!谢谢
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);
}