向 SQL 查询的 WHERE 子句添加参数

发布于 2024-10-09 09:37:49 字数 750 浏览 3 评论 0原文

我正在使用 ADO.NET 通过 ODBC 连接到 Oracle DB。除了使用简单的 SQL 查询绑定参数之外,一切都工作正常:

Connection.Open();
IDbCommand command = Connection.CreateCommand();
command.CommandText = "SELECT length FROM activity_type WHERE name = :name_of_activity";
var parameter = command.CreateParameter();
parameter.ParameterName = ":name_of_activity";
parameter.Value = "Short_break";
command.Parameters.Add(parameter);
int result = Convert.ToInt32(command.ExecuteScalar());
Connection.Close();

它总是返回 0 个结果(来自 ExecuteScalar() 的 null - 与读取器的结果相同)。但如果我像这样放置一个简单的 SQL 查询:command.CommandText = "SELECT length FROM Activity_type WHERE name = 'Short_break'" 它会像魅力一样工作。更重要的是,我在 INSERT INTO 子句的代码中使用了类似的结构,而且效果很好。

我错过了什么吗?

I'm using ADO.NET to connect to an Oracle DB through ODBC. Everything is working fine, besides binding parameters with a simple SQL query:

Connection.Open();
IDbCommand command = Connection.CreateCommand();
command.CommandText = "SELECT length FROM activity_type WHERE name = :name_of_activity";
var parameter = command.CreateParameter();
parameter.ParameterName = ":name_of_activity";
parameter.Value = "Short_break";
command.Parameters.Add(parameter);
int result = Convert.ToInt32(command.ExecuteScalar());
Connection.Close();

It always returns 0 results (a null from ExecuteScalar() - the same from a reader). But if I'd put a straightforward SQL query like this: command.CommandText = "SELECT length FROM activity_type WHERE name = 'Short_break'" it would work like a charm. Whats more, I used similar constructions all over the code for INSERT INTO clauses, and they were OK.

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-10-16 09:37:49

来自 的文档OdbcCommand.Parameters

当 CommandType 设置为 Text 时,ODBC 的 .NET Framework 数据提供程序不支持将命名参数传递到 SQL 语句或 OdbcCommand 调用的存储过程。在这两种情况下,请使用问号 (?) 占位符。例如:

从客户中选择 * WHERE CustomerID = ?

换句话说,您的代码应该如下所示:(

Connection.Open();
IDbCommand command = Connection.CreateCommand();
command.CommandText = "SELECT length FROM activity_type WHERE name = ?";
var parameter = command.CreateParameter();
parameter.Value = "Short_break";
command.Parameters.Add(parameter);
int result = Convert.ToInt32(command.ExecuteScalar());
Connection.Close();

请注意,您可能应该考虑使用 using 语句...否则,如果这引发异常,您将不会关闭联系。)

From the docs for OdbcCommand.Parameters:

When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder. For example:

SELECT * FROM Customers WHERE CustomerID = ?

In other words, your code should look like this:

Connection.Open();
IDbCommand command = Connection.CreateCommand();
command.CommandText = "SELECT length FROM activity_type WHERE name = ?";
var parameter = command.CreateParameter();
parameter.Value = "Short_break";
command.Parameters.Add(parameter);
int result = Convert.ToInt32(command.ExecuteScalar());
Connection.Close();

(You should probably be considering using using statements, mind you... otherwise if this throws an exception, you won't be closing the connection.)

北方的巷 2024-10-16 09:37:49

根据你的描述,问题只能出在参数上。

也许存在某种奇怪的混合,例如 SQL Server 上的 varchar 和 nvarchar 之间的混合,换句话说,尝试显式设置数据类型。

From what you're describing the problem can only be in the parameter.

Maybe some kind of strange mixup like between varchar and nvarchar on SQL Server, in other words try setting the datatype explicitely.

红墙和绿瓦 2024-10-16 09:37:49

尝试更改此行:

parameter.ParameterName = ":name_of_activity";

换句话说

parameter.ParameterName = "name_of_activity";

,删除冒号字符

Try to change this line:

parameter.ParameterName = ":name_of_activity";

to

parameter.ParameterName = "name_of_activity";

In other words remove colon character

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文