Error entering in batch using Oraclecommand

Asked

Viewed 282 times

0

I need to optimize a load application that runs through a database and writes to another database, but this has to be done through an application Windows Forms.

I’m doing using the ArrayBindCount of Oraclecommand, but is returning zero reference error when passing the parameter, however I have already checked and the vector to enter when creating the parameter is normal and has no null object in the line that fires the problem:

OracleConnection con = new OracleConnection(txtDestino.Text);
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.BindByName = true;
cmd.ArrayBindCount = totalRegistros;
cmd.CommandText = @"insert into solicitacao (cd_codigo) values (:cdCodigo)";
List<int> array_CodSolicitacaoNovo = new List<int>();
array_CodSolicitacaoNovo.Add(12);
array_CodSolicitacaoNovo.Add(13);
array_CodSolicitacaoNovo.Add(14);
//ERRO NESTA LINHA ABAIXO
cmd.Parameters.Add(new OracleParameter("v_soli_cd_codigo", array_CodSolicitacaoNovo.ToArray());

//Executado apenas uma vez
con.Open();
cmd.ExecuteNonQuery();
con.Close();

If it has no null object the error would be inside the method new OracleParameter? I am using the dll Oracle.Dataaccess 4.112.3.0.

Note: I can’t use Stored Procedures to do the Inserts.

2 answers

0

I was able to find a solution, the problem was to specify the type of the data and whether it was input or output when creating the parameter:

cmd.Parameters.Add(new OracleParameter("v_soli_cd_codigo", OracleDbType.Int32, array_CodSolicitacaoNovo.ToArray(), System.Data.ParameterDirection.Input));

When running Insert no longer gave error and saved all vector data. I don’t know why when the input is a vector I need to use the method Overload, but it worked. the/

0

Rodrigo, why don’t you use foreach to fill in primitive types, or pass a string array to a stored Procedure?

Take a look at what the foreach would look like:

OracleConnection con = new OracleConnection(txtDestino.Text);
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.BindByName = true;
cmd.ArrayBindCount = totalRegistros;
cmd.CommandText = @"insert into solicitacao (cd_codigo) values (:cdCodigo)";
List<int> array_CodSolicitacaoNovo = new List<int>();
array_CodSolicitacaoNovo.Add(12);
array_CodSolicitacaoNovo.Add(13);
array_CodSolicitacaoNovo.Add(14);


foreach (var item in array_CodSolicitacaoNovo)
{
     cmd.Parameters.Add(new OracleParameter("v_soli_cd_codigo", item.ToString());
}

//Executado apenas uma vez
con.Open();
cmd.ExecuteNonQuery();
con.Close();

I hope I helped. Abs

  • Hi Andre, in fact this way you suggested it is not making a batch Insert, this is the model I had before and needed to change to optimize. I have a database of over 200,000 items and making a bank call for each Insert turns out to be bad. That’s why you have to pass the array of values directly in the creation of the Parameter, but you are giving error inside. :)

  • Andre, as for the use of Procedure, I forgot to comment but I can not use due to constraints of the bank environment and technical requirements.

  • I have a routine that does this, but instead of running it like this, I run a script using Stringbuilder and add several lines of INSERT, for example and run at once, with the possibility to transact the execution.

  • But is the execution of all this line also done via code? Or do you generate this file as a txt and take it and run through pl/sql for example?

  • Something like this: //Script creation Stringbuilder scriptSQL = new Stringbuilder(); scriptSQL.Add(string.Format("Insert into request (cd_code) values ({0})", your information); scriptSQL.Add(string.Format("Insert into request (cd_code) values ({0})", information); scriptSQL.Add(string.Format("Insert into request (cd_code) values ({0})", your information); Script execution Executenonquery(scriptSQL.Tostring())

  • Does this work via Oraclecommand? Multiple nested commands? I know it works directly through pl/sql. Working would be a good alternative, but I would like to take advantage of the code I already have without having to make many concatenations that increase the risk of error and hinder maintenance. But I appreciate all your support. :)

  • In the case of Oracle, just place the semicolon at the end of each line.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.