使用 ODP.NET 按名称绑定查询参数

Binding query parameters by name with ODP.NET(使用 ODP.NET 按名称绑定查询参数)
本文介绍了使用 ODP.NET 按名称绑定查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用适用于 Oracle 的 Microsoft ADO.NET 提供程序 (System.Data.OracleClient).我知道它肯定不是最好的 Oracle 提供程序,它 很快就会被弃用,我应该改用 Oracle 的 ODP.NET.我仍然使用 MS 提供程序的原因是因为 ODP.NET 按位置绑定参数,而不是按名称.当您在查询中使用许多参数时,这确实是一个 PITA,因为您必须小心以正确的顺序添加它们,这很容易导致错误.当你在同一个查询中多次使用同一个参数时,这也很烦人,例如:

I'm currently using the Microsoft ADO.NET provider for Oracle (System.Data.OracleClient). I'm aware that it is certainly not the best Oracle provider available and that it will soon be deprecated, I should be using Oracle's ODP.NET instead. The reason why I still use the MS provider is because ODP.NET binds parameters by position, not by name. This can really be a PITA when you use many parameters in a query, because you have to be careful to add them in the right order, which can easily lead to bugs. It's also annoying when you use the same parameter multiple times in the same query, for instance :

SELECT A,B,C FROM FOO WHERE X = :PARAM_X OR :PARAM_X = 0

使用 ODP.NET,我必须在 OracleCommand 中添加两个参数,我认为这很愚蠢...

With ODP.NET, I have to add two parameters to the OracleCommand, which I think is stupid...

ODP.NET 的 OracleCommand 具有更改该默认行为的属性:BindByName.当设置为true时,参数由名称绑定,这就是我想要的.不幸的是,这并没有真正帮助我,因为:

ODP.NET's OracleCommand has a property to change that default behavior : BindByName. When set to true, the parameters are bound by name, which is what I want. Unfortunately this doesn't really help me, because :

  • 默认设置为false
  • 我几乎从不明确使用具体的 ADO.NET 类,我更喜欢使用 ADO.NET 2.0 抽象层(DbProviderFactoryDbConnectionDbCommand...) 以减少与任何特定 RDBMS 的耦合.所以我无法访问 BindByName 属性,除非我明确地转换为 OracleCommand,从而失去所有好处或抽象.
  • 使用 ASP.NET SqlDataSource 时,我不会自己创建 DbCommand,因此我没有机会将 BindByName 设置为 true(我可以在 Selecting 事件中执行此操作,但是为每个 SqlDataSource 做这件事真的很痛苦...)
  • It is set to false by default
  • I almost never use concrete ADO.NET classes explicitly, I prefer to use ADO.NET 2.0 abstraction layer (DbProviderFactory, DbConnection, DbCommand...) to reduce coupling to any specific RDBMS. So I don't have access to the BindByName property, unless I cast explicitly to OracleCommand, losing all the benefits or the abstraction.
  • When using an ASP.NET SqlDataSource, I don't create the DbCommand myself, so I don't get a chance to set BindByName to true (I could do it in the Selecting event, but it really is a pain to do it for each SqlDataSource...)

我应该如何处理这个问题?某处是否有 BindByNameByDefault 设置?(我没有找到类似的东西,但我可能错过了......)

How am I supposed to handle that issue ? Is there a BindByNameByDefault setting somewhere ? (I didn't find anything like that, but I may have missed it...)

推荐答案

我认为您可以创建自己的提供程序,使用您想要使用的默认值.您可以通过从 odp.net 继承所有类来轻松创建该提供程序,只需调整一些属性,如 BindByName.

I think you can create your own provider that uses the defaults you want to use. You could create that provider easily by inheriting all the classes from odp.net, just adjust some properties like BindByName.

DbProviderfactory 将创建您的类而不是普通的 odp.net 类.

The DbProviderfactory will create your classes instead of the normal odp.net classes.

这篇关于使用 ODP.NET 按名称绑定查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)