4

I have a table defined in oracle 11g with below statement:

  CREATE TABLE "TESTUSER"."TestTableOracleWriter" 
  ("name" VARCHAR2(100 BYTE), 
   "group" VARCHAR2(100 BYTE), 
   "number" NUMBER(*,0), 
   "creation" DATE, 
   "sliceidentifier" RAW(100), 
   CONSTRAINT "TESTTABLEORACLEWRITER_PK" PRIMARY KEY ("name")) 

And I am using the following code snippet to update the table with content in the dataTable:

    private void BatchInsert(DbConnection connection, DbTransaction transaction, DataTable dataTable, string tableName)
    {
        DbDataAdapter adapter = ProviderFactories.GetFactory("Oracle.DataAccess.Client").CreateDataAdapter();
        DbCommand insertCommand = connection.CreateCommand();

        DbParameter parameter1 = insertCommand.CreateParameter();
        parameter.DbType = DbType.String;
        parameter.ParameterName = "@name";
        parameter.SourceColumn = "name";
        insertCommand.Parameters.Add(parameter);

        DbParameter parameter2 = insertCommand.CreateParameter();
        parameter2.DbType = DbType.String;
        parameter2.ParameterName = "@group";
        parameter2.SourceColumn = "group";
        insertCommand.Parameters.Add(parameter2);

        DbParameter parameter3 = insertCommand.CreateParameter();
        parameter3.DbType = DbType.Int32;
        parameter3.ParameterName = "@number";
        parameter3.SourceColumn = "number";
        insertCommand.Parameters.Add(parameter3);

        DbParameter parameter4 = insertCommand.CreateParameter();
        parameter4.DbType = DbType.DateTime;
        parameter4.ParameterName = "@creation";
        parameter4.SourceColumn = "creation";
        insertCommand.Parameters.Add(parameter4);

        insertCommand.CommandType = CommandType.Text;
        insertCommand.CommandText = "INSERT INTO \"TestTableOracleWriter\" (\"name\", \"group\", \"number\", \"creation\") VALUES (:name, :group, :number, :creation)";
        insertCommand.Transaction = transaction;
        insertCommand.UpdatedRowSource = UpdateRowSource.None;

        adapter.InsertCommand = insertCommand;
        adapter.UpdateBatchSize = 0;    
        adapter.Update(dataTable);
    }

But sometimes the code will fail with "ORA-01745: invalid host/bind variable name", I've searched on the internet and found some materials saying it has something to do with the oracle reserve word. From the link, "name", "group" and "number" is marked as reserve word. I can change my table column names to make the code work.

But the strangest thing is that the code does not fail all the time, it only fails when dataTable cotains only one row, in other scenarios, it works as expected. Anyone has ideas about that?

Chasefornone
  • 747
  • 1
  • 7
  • 15

1 Answers1

6

You can not use key word as parameter name. Don't use group and number as parameter name

Troopers
  • 5,127
  • 1
  • 37
  • 64