-2

I'm trying to insert into table in a parallel loop.

public static void idToHashEncoder()
{
   string sql = "";
   Stopwatch sw = new Stopwatch();
   sw.Start();

   Parallel.For(0, 1001, i =>
   {
      sql = string.Format(@"INSERT INTO ID_TO_HASH(ID, HASH) VALUES({0},
           {1})", i.ToString(), i.ToString().GetHashCode());

     GeneralDbExecuterService.executeSqlSelectDataScript(sql.ToString());

   });

   sw.Stop();
   TimeSpan elapsedTime = sw.Elapsed;
 }

executeSqlSelectDataScript:

public static DataTable executeSqlSelectDataScript(string sql) 
{
    //String sConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ORADB"].ConnectionString;
    // OleDbConnection myConnection = new OleDbConnection(sConnectionString);
    //OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
    OracleConnection myConnection = new OracleConnection(sConnectionString);
    OracleCommand myCommand = new OracleCommand(sql, myConnection);

    try
    {
       //myCommand.Parameters.Add("@p1", OleDbType.Char, 5).Value = "Test%";
       myConnection.Open();
       //OleDbDataReader myReader = myCommand.ExecuteReader();
        OracleDataReader myReader = myCommand.ExecuteReader();

        var dataTable = new DataTable();
        dataTable.Load(myReader);
        myReader.Close();
        return dataTable;

   }
   catch (Exception ex)
   {
       logger.Error(ex.Message + "sql : " + sql);
       throw ex;
   }
   finally
   {
      myConnection.Close();
   }
}

I'm getting this:

An exception of type 'System.TypeInitializationException' occurred in PRD.dll but was not handled in user code

Any economical solution will be welcome

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • `TypeInitializationException` means a static constructor or static initialization of some field threw an exception, neither of which is present in the code in the question. You need to narrow it down further. – Lasse V. Karlsen Dec 05 '17 at 09:05
  • Instead of `throw ex;`, you should use `throw;`. – Uwe Keim Dec 05 '17 at 09:13

1 Answers1

-1

You are trying INSERT but use Datatable for result. Datatable is using for SELECT. You need below command for insert because of non result:

myCommand.ExecuteNonQuery();
mkysoft
  • 5,392
  • 1
  • 21
  • 30