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

后端 未结 7 1612
粉色の甜心
粉色の甜心 2020-12-19 06:25

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

相关标签:
7条回答
  • 2020-12-19 06:29

    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>
    
    0 讨论(0)
  • 2020-12-19 06:29

    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 };
    
    0 讨论(0)
  • 2020-12-19 06:31

    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.

    0 讨论(0)
  • 2020-12-19 06:37

    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;
                }
            }
    
    0 讨论(0)
  • 2020-12-19 06:44

    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:

    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.

    0 讨论(0)
  • 2020-12-19 06:47

    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;
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题