Is there a way to force OracleCommand.BindByName t

2020-03-19 02:46发布

Since the System.Data.OracleClient library 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.OracleCommand as opposed to using an intermediate data layer.

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?

7条回答
劳资没心,怎么记你
2楼-- · 2020-03-19 02:51

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

        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;
            }
        }
查看更多
混吃等死
3楼-- · 2020-03-19 02:51

With Oracle.ManagedDataAccess.Client, you can configure in 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>
查看更多
放荡不羁爱自由
4楼-- · 2020-03-19 02:54

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:

protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    Oracle.DataAccess.Client.OracleCommand b_OracleCommand = 
                  (Oracle.DataAccess.Client.OracleCommand)e.Command;
    b_OracleCommand.BindByName = true;
}
查看更多
你好瞎i
5楼-- · 2020-03-19 02:58

To reduce # lines of code

VB.NET

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

C#

OracleCommand command = new OracleCommand(query, connection) { CommandType = CommandType.StoredProcedure, BindByName = true };
查看更多
贼婆χ
6楼-- · 2020-03-19 03:05

I didn't try it but,

I have seen something like

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

Maybe it can help.

查看更多
你好瞎i
7楼-- · 2020-03-19 03:05

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

protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    ((Oracle.ManagedDataAccess.Client.OracleCommand)e.Command).BindByName = true;
    // ...
}
查看更多
登录 后发表回答