简体   繁体   English

C#Npgsql,支持过程调用吗?

[英]C# Npgsql, Call of procedure supported ?

with regards to the new update of postgresSQL (ver 11). 关于postgresSQL的新更新(版本11)。 Procedures are now supported. 现在支持过程。 Procedures can be created using the CREATE PROCEDURE command and executed using the CALL command. 可以使用CREATE PROCEDURE命令创建过程,并使用CALL命令执行过程。

CREATE PROCEDURE MyInsert(_sno text, _eid text, _sd date)
LANGUAGE SQL
AS $$
    INSERT INTO app_for_leave(sno, eid, sd)
    VALUES(_sno, _eid, _sd);   
$$;

CALL MyInsert('4','5','2013-04-04');

Is the "CALL" supported right now ? 现在支持“致电”吗?

Currently i can only make it work by building a String as a command and then executing it. 目前,我只能通过将String作为命令构建然后执行来使其工作。 eg 例如

using (var conn = new NpgsqlConnection(_connectionString))
{
         conn.Open();
          using (var cmd = conn.CreateCommand())
          {
               cmd.CommandText = "CALL MyInsert('4','5','2013-04-04')";
               cmd.ExecuteNonQuery();
          }
         conn.Close();
}

Please do let me know if there is a better way of executing the "MyInsert" without building the query string. 请告诉我是否有一种更好的方式来执行“ MyInsert”而不构建查询字符串。 This is susceptible to sql injection as it is not a prepared statement. 由于它不是预准备的语句,因此容易受到sql注入的影响。

Thanks in advance ! 提前致谢 !

Creating a regular command with CALL proc_name() is perfectly fine for executing stored procedures - there's nothing else that's needed really. 使用CALL proc_name()创建常规命令非常适合执行存储过程-确实不需要任何其他操作。 Nothing about this is more or less susceptible to SQL injection: you can still pass parameters as usual by specifying parameter placeholders (eg @param1, @param2). 几乎没有什么影响SQL注入的:您仍然可以通过指定参数占位符(例如,@ param1,@ param2)像往常一样传递参数。 Note that parameters and placeholders also have nothing to do with prepared statements - it's perfectly valid to send parameters (and therefore be protected against SQL injection) with a non-prepared statements 请注意,参数和占位符也与准备好的语句无关-通过非准备好的语句发送参数(因此可以防止SQL注入)是完全有效的

The PostgreSQL protocol doesn't have any special provisions for calling functions or procedures. PostgreSQL协议对调用函数或过程没有任何特殊规定。 Even when you use CommandType.StoredProcedure for calling functions, all this does is make Npgsql construct a SELECT func_name() under the hood and send it as a normal command. 即使当您使用CommandType.StoredProcedure调用函数时,所有这些操作也使Npgsql在后台构造SELECT func_name()并将其作为常规命令发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM