oracle 对于 ODP.NET,有没有办法强制 OracleCommand.BindByName 默认为 true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2177733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:55:09  来源:igfitidea点击:

Is there a way to force OracleCommand.BindByName to be true by default for ODP.NET?

.netoraclerefactoringodp.netsystem.data.oracleclient

提问by rjzii

Since the System.Data.OracleClientlibrary has been deprecated, we are in the process of migrating our code base to use Oracle Data Provider for .NET(ODP.NET) instead. One of the issues that we have encountered is that the System.Data.OracleClient uses parameter name binding as opposed to binding by position and all of the code directly access the System.Data.OracleClient.OracleCommandas opposed to using an intermediate data layer.

由于System.Data.OracleClient库已被弃用,我们正在迁移我们的代码库以改用Oracle Data Provider for .NET(ODP.NET)。我们遇到的问题之一是 System.Data.OracleClient 使用参数名称绑定而不是按位置绑定,并且所有代码直接访问System.Data.OracleClient.OracleCommand而不是使用中间数据层。

Since there is quite a bit of code, is there an easy way to force the ODP.NET OracleCommand.BindByName to be true by default, or must we go through and set the value each time that it is used? Failing at that, is there an easy way to insert that line of code in Visual Studio 2008?

由于有相当多的代码,是否有一种简单的方法可以强制 ODP.NET OracleCommand.BindByName 默认为 true,或者我们必须在每次使用时都经过并设置该值?如果失败,是否有一种简单的方法可以在 Visual Studio 2008 中插入该行代码?

回答by user925169

I didn't try it but,

我没试过,但是

I have seen something like

我见过类似的东西

"cmd.GetType().GetProperty("BindByName").SetValue(cmd,true,null);" in PetaPoco.cs file.

cmd.GetType().GetProperty("BindByName").SetValue(cmd,true,null);”在 PetaPoco.cs 文件中。

Maybe it can help.

也许它可以提供帮助。

回答by BrandonZeider

I know this thread is old, but I had the same problem today and thought I would share my solution in case someone else had this problem. Since OracleCommand is sealed (which sucks), I created a new class that encapsulates the OracleCommand, setting the BindByName to true on instantiation. Here's part of the implementation:

我知道这个线程很旧,但我今天遇到了同样的问题,我想我会分享我的解决方案,以防其他人遇到这个问题。由于 OracleCommand 是密封的(这很糟糕),我创建了一个封装 OracleCommand 的新类,在实例化时将 BindByName 设置为 true。这是实现的一部分:

public class DatabaseCommand
{
    private OracleCommand _command = null;

    public DatabaseCommand(string sql, OracleConnection connection)
    {
        _command = new OracleCommand(sql, connection)
        {
            BindByName = true
        };
    }

    public int ExecuteNonQuery()
    {
        return _command.ExecuteNonQuery();
    }

    // Rest of impl removed for brevity
}

Then all I had to do to cleanup the commands was do a search for OracleCommand and replace with DatabaseCommand and test.

然后我需要做的就是清理命令,搜索 OracleCommand 并替换为 DatabaseCommand 并进行测试。

回答by Bore

I had the same problem with SqlDataSource Update commands after porting ASPX code to Oracle.DataAcees.Client and solved it by changing OracleCommand.BindByName property in SqlDataSource OnUpdating handler like this:

将 ASPX 代码移植到 Oracle.DataAcees.Client 后,我​​遇到了与 SqlDataSource Update 命令相同的问题,并通过更改 SqlDataSource OnUpdating 处理程序中的 OracleCommand.BindByName 属性解决了这个问题,如下所示:

protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    Oracle.DataAccess.Client.OracleCommand b_OracleCommand = 
                  (Oracle.DataAccess.Client.OracleCommand)e.Command;
    b_OracleCommand.BindByName = true;
}

回答by DerSkythe

Add partial class for your TableAdapter, and add method, or property, as you want, with this code:

为您的 TableAdapter 添加部分类,并根据需要使用以下代码添加方法或属性:

        for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1))
        {
            if ((this.CommandCollection[i] != null))
            {
                ((global::Oracle.DataAccess.Client.OracleCommand)(this.CommandCollection[i])).BindByName = value;
            }
        }

回答by ARoller

I resolved this issue setting the BindByName property in the handler of the SqlDataSource Updating event:

我通过在 SqlDataSource 更新事件的处理程序中设置 BindByName 属性解决了这个问题:

protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    ((Oracle.ManagedDataAccess.Client.OracleCommand)e.Command).BindByName = true;
    // ...
}

回答by Sadao

With Oracle.ManagedDataAccess.Client, you can configure in app.config:

使用 Oracle.ManagedDataAccess.Client,您可以在 app.config 中进行配置:

<oracle.manageddataaccess.client>
<version number="*">
  <dataSources>
    <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) " />
  </dataSources>
  <settings>
    <setting name="BindByName" value="True"/>
  </settings>
</version></oracle.manageddataaccess.client>

回答by Glenn

To reduce # lines of code

减少#行代码

VB.NET

网络

Dim command As OracleCommand = New OracleCommand(query, connection) With {.CommandType = CommandType.StoredProcedure, .BindByName = True}

C#

C#

OracleCommand command = new OracleCommand(query, connection) { CommandType = CommandType.StoredProcedure, BindByName = true };