简体   繁体   English

System.Data.Common.DbCommand:参数化查询作为过程参数?

[英]System.Data.Common.DbCommand: Parameterized Query as Procedure Parameter?

I have procedure in PostgreSQL defined as: 我将PostgreSQL中的过程定义为:

CREATE OR REPLACE FUNCTION CreateCursorC(text, text)
 RETURNS text
 LANGUAGE c
AS '$libdir/mylibs', $function$createcursorc$function$

Execute example: 执行示例:

SELECT CreateCursorC('cursor_name', 'SELECT a FROM x WHERE a=''text''');

Of course I would like to use parameters (DbCommand.Parameters). 当然我想使用参数(DbCommand.Parameters)。 Like this: 像这样:

SELECT CreateCursorC($1, 'SELECT a FROM x WHERE a=$2');

Unfortunately it's not working because parameter $2 is in quotes. 不幸的是它没有用,因为参数$ 2在引号中。 Is there a way to accomplished this task using parameters and not by writing custom SQL escaping function ? 有没有办法使用参数完成此任务,而不是通过编写自定义SQL转义函数

I tried to get an answer at Devart Forum, but no luck: Parameterized Query as Procedure Parameter? 我试图在Devart论坛上得到答案,但没有运气: 参数化查询作为程序参数? | | Devart Forums Devart论坛

I think I understand your issue. 我想我理解你的问题。 I ran into a similar problem when trying to setup a parameterized query that included a LIKE expression in MySQL, via C#. 当尝试通过C#设置包含MySQL中的LIKE表达式的参数化查询时,我遇到了类似的问题。

The trick that I found, in the case of the LIKE expression, was to make the % characters part of the parameter. 在LIKE表达式的情况下,我发现的技巧是使%字符成为参数的一部分。 In your case, this same type of logic may work for your quoted text. 在您的情况下,这种相同类型的逻辑可能适用于您的引用文本。

Here's a snippet of code showing what I did in my case: 这是一段代码,展示了我在我的案例中所做的事情:

IDBCommandParameters cmdParams = dbContext.CreateDBCommandParameters();

cmdParams.AddParameter(QueryConstants.likeParam, string.Format("%{0}%", likeFilter));

List<TQueryResult> companies = LoadModelList<TQueryResult>(dbContext.Find(QueryConstants.findCompaniesLikeStatement, cmdParams, false), "");

return companies;

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

相关问题 system.data.common.dbCommand 的 eExecuteDataSet? - eExecuteDataSet for system.data.common.dbCommand? “参数数量与存储过程的值数量不匹配” System.Data.Common.DbCommand细微差别 - “The number of parameters does not match number of values for stored procedure” System.Data.Common.DbCommand nuance 从System.Data.Common.DbCommand继承,Parameters属性为null - Inheriting from System.Data.Common.DbCommand, Parameters property is null 非静态字段,方法或属性&#39;System.Data.Common.DbCommand.ExecuteScalar()&#39;需要对象引用 - An object reference is required for the non-static field, method, or property 'System.Data.Common.DbCommand.ExecuteScalar()' 将参数化的SQL查询作为参数传递给存储过程不起作用 - Passing parameterized sql query as parameter to a stored procedure not working 在select中带有参数的参数化查询返回无效数据 - Parameterized query with parameter in select returns invalid data 添加参数到DbCommand的例外情况 - Exception in Add Parameter To DbCommand 在参数化查询中缺少必需的参数? - Missing Required Parameter in Parameterized Query? DbCommand和参数化SQL,ORACLE与SQL Server - DbCommand and parameterized SQL, ORACLE vs SQL Server 参数在DBCommand中添加导致超时 - Parameter Add Causing Timeout in DBCommand
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM