Search code examples
c#oracle-databaseupdatesbulk

Oracle: Bulk update of records from c#


I have a requirement where I have to perform bulk update of records in Oracle using c#. I'm trying to achieve this by passing an array of record ids that have to be updated back to oracle,but the code is not working.

The latest code that I have used is-

List<Int64> listTransId = new List<Int64>();
while (reader.Read())
{
  listTransId.Add(Convert.ToInt64(reader["TOLL_TX_SEQ_NUM"]));
}

Int64[] arrTOLL_TX_SEQ_NUM = new Int64[listTransId.Count];
arrTOLL_TX_SEQ_NUM = listTransId.ToArray();

OracleTransaction txn =  connection.BeginTransaction(IsolationLevel.ReadCommitted);
OracleCommand updateCmd = new OracleCommand(@" BEGIN UPDATE TOLL_TRANSACTION SET CCH_EXPORT_DATETIME = SYSDATE WHERE TOLL_TX_SEQ_NUM = :TOLL_TX_SEQ_NUM; END;");
updateCmd.CommandType = CommandType.Text;
updateCmd.Connection = connection;
//update Cmd.BindByName = true;
update Cmd.ArrayBindCount = arrTOLL_TX_SEQ_NUM.Length;

OracleParameter TOLL_TX_SEQ_NUM = new OracleParameter("TOLL_TX_SEQ_NUM", OracleDbType.Int64);
TOLL_TX_SEQ_NUM.Direction = ParameterDirection.Input;
TOLL_TX_SEQ_NUM.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
TOLL_TX_SEQ_NUM.Value = arrTOLL_TX_SEQ_NUM;
TOLL_TX_SEQ_NUM.Size = arrTOLL_TX_SEQ_NUM.Length;

updateCmd.Parameters.Add(TOLL_TX_SEQ_NUM);
Console.WriteLine("Connection state - " + connection.State);
updateCmd.ExecuteNonQuery();
txn.Commit();

connection.Close();

Solution

  • use "array binding" with a simply update statement.

    http://www.oracle.com/technetwork/issue-archive/2009/09-sep/o59odpnet-085168.html