并行循环插入表

时间:2017-12-05 08:36:51

标签: c# oracle task-parallel-library parallel.foreach gethashcode

我正在尝试以并行循环方式插入表中。

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();
   }
}

我收到了

  

发生了'System.TypeInitializationException'类型的异常   PRD.dll但未在用户代码中处理

欢迎任何经济的解决方案

1 个答案:

答案 0 :(得分:-1)

您正在尝试INSERT,但使用Datatable作为结果。 Datatable用于SELECT。由于没有结果,您需要以下命令进行插入:

myCommand.ExecuteNonQuery();