绑定参数给OLEDB命令抛出错误

编程入门 行业动态 更新时间:2024-10-10 04:27:03
本文介绍了绑定参数给OLEDB命令抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用AS 400 OLEDB与.NET。它使用'?'而不是'@param用命令绑定参数现在有一种情况,命令是像

I am using AS 400 OLEDB with .NET. It uses '?' instead of '@param to bind parameters with a command Now there is a situation where the command is like

SELECT ... FROM (SELECT ... ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum FROM Employees e ) as DerivedTableName WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1

我的命令变成

Now my command becomes

SELECT ... FROM (SELECT ... ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum FROM Employees e ) as DerivedTableName WHERE RowNum BETWEEN ? AND (? + ?) - 1

现在当我绑定参数

myCommand.Parameters.Add(new OleDbParameter("?",startRowIndex)); myCommand.Parameters.Add(new OleDbParameter("?", startRowIndex)); myCommand.Parameters.Add(new OleDbParameter("?", MaximumRows));

它会引发错误

SQL0417: Combination of parameter markers not valid. Cause . . . . . : The statement string specified as the object of a PREPARE statement contains a predicate or expression where parameter markers have been used as operands of the same operator. The following restrictions apply to the use of parameter markers: -- Both the operands in a predicate cannot be parameter markers. For example, specifying predicates of the form: ? = ? or ? = ( SELECT ? FROM x ) are not valid.

如何在这种情况下绑定参数?我想避免sql注入:)

How do I bind parameters in this situation ? I want to avoid sql injection :)

推荐答案

尝试更改参数的名称

myCommand.Parameters.Add(new OleDbParameter("@startRowIndex",startRowIndex)); myCommand.Parameters.Add(new OleDbParameter("@startRowIndex2", startRowIndex)); myCommand.Parameters.Add(new OleDbParameter("@MaximumRows", MaximumRows));

但保留SQL。

更多推荐

绑定参数给OLEDB命令抛出错误

本文发布于:2023-11-28 17:03:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   抛出   命令   错误   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!