Oracle.ManagedDataAccess.Client支持ExecuteNonQuery()

时间:2018-01-07 19:24:26

标签: c# asp.net-mvc oracle11g

我使用System.Data.OracleClient并且存在使用ExecuteNonQuery()的存储过程。现在我正在使用Oracle.ManagedDataAccess.Client所以我的问题是我可以在ExecuteNonQuery()中使用Oracle.ManagedDataAccess.Client吗?

我之前的代码是

   Database db = DatabaseFactory.CreateDatabase();
   DbCommand objComm =  db.GetStoredProcCommand("package_name.sp", ab, ab1, ab2, ab3, ab4, ab5, ab6);
   var result = new Collection<Alarm>();
   db.ExecuteNonQuery(objComm); 

现在我的代码是

  OracleCommand cmd = new OracleCommand();
  cmd.Connection = conn;
  cmd.CommandText = commandText;
  cmd.CommandType =CommandType.StoredProcedure;
  cmd.Parameters.Add("ab", OracleDbType.Varchar2, ParameterDirection.Input);
  cmd.Parameters.Add("ab1", OracleDbType.Varchar2, ParameterDirection.Input);
  cmd.Parameters.Add("ab2", OracleDbType.Varchar2, ParameterDirection.Input);  
  cmd.Parameters.Add("ab3", OracleDbType.Varchar2, ParameterDirection.Input);
  cmd.Parameters.Add("ab4", OracleDbType.Varchar2, ParameterDirection.Input);
  cmd.ExecuteNonQuery(); 

此更新的代码是否有效?

2 个答案:

答案 0 :(得分:0)

当然,我正在从Oracle.DataAccess / System.Data.OracleClient转换为Oracle.ManagedDataAccess。

我在这个过程中发现了一些问题:

1.  Make sure you specify your data type in the parameters, otherwise, it has a tendency to fail.
2.  An older version of managed has a CLOB bug that I ran into when you try to select data.  Make sure you get the latest from github.
3.  By default, the tnsnames/ldap/sqlnet location defaults to the application directory in the managed situation.  If you assign ORACLE_HOME environmental variable at the windows level, it'll fix that.
4.  Does the server have the full version of Oracle client?  Do they have both a 64-bit and 32-bit version?  You'll want to make sure you set the environmental variable in the application then to and use the right one, otherwise you'll run into Crystal issues.
5.  Long running executions sometimes have issues if you are using Oracle database 11.1 - https://stackoverflow.com/questions/29847444/odp-net-oracle-manageddataaccess-causes-ora-12537-network-session-end-of-file

示例参数添加

cmd.Parameters.Add("parametername", OracleDbType.Varchar2, 
  parametervalue, ParameterDirection.Input)

请记住,Oracle.ManagedDataAccess继承/实现与System.Data.OracleClient相同的类/接口的结构,因此您会看到很多相似之处,但它不会完全相同。

答案 1 :(得分:0)

请注意,.BindByName默认为false

不按名称绑定参数时,Oracle.ManagedDataAccess会按照执行cmd.Paramaters.Add()调用的顺序来绑定参数。