2012-03-29 8 views
0

次のコードは、一度に100個のINSERT文を実行するためにどのように変更できますか?また、それは100%以上を実行する最後の数を実行することができる必要があります。今日は非常に遅い脳の日です。100回繰り返しごとに関数を実行しますが、最後のものもキャッチします

if (oleDataBaseConnection.HasRows()) 
{ 
    int counter = 0; 

    Spinner spinner = new Spinner(); 

    StringBuilder postgresQuery = new StringBuilder(); 

    Dictionary<string, string> postgreSQLQueries = TypeConversion.GetQueryDictionary("POSTGRESQL"); 

    while (oleDataBaseConnection.NextRecord()) 
    { 
     string postgreSQLInsertQuery; 

     postgreSQLQueries.TryGetValue("INSERT", out postgreSQLInsertQuery); 

     postgreSQLInsertQuery = postgreSQLInsertQuery.Replace("{0}", tableName); 
            spinner.Turn(); 

     postgresQuery.Append(postgreSQLInsertQuery); 

     postgresQuery.Append("("); 

     int columnCounter = 0; 

     //add a column parameter to query for each of our columns 
     foreach (KeyValuePair<string, string> t in destinationColumnData) 
     { 
      postgresQuery.Append(t.Key + ","); 
      columnCounter++; 
     } 

     postgresQuery = postgresQuery.Remove(postgresQuery.Length - 1, 1); 
     postgresQuery.Append(") "); 

     postgresQuery.Append("VALUES ("); 

     //Loop through values for column names/types 
     for (int i = 0; i < columnCounter; i++) 
     { 
      if (String.IsNullOrEmpty(oleDataBaseConnection.GetFieldById(i))) 
      { 
       postgresQuery.Append("NULL, "); 
      } 
      else 
      { 
       switch (foobar[i].ToUpper()) 
       { 
        case "TEXT": 
         postgresQuery.Append("$$" + oleDataBaseConnection.GetFieldById(i) + "$$, "); 
         break; 
        case "GEOMETRY": 
         postgresQuery.Append("ST_GeomFromText('" + oleDataBaseConnection.GetFieldById(i) + "'), "); 
         break; 
        default: 
         postgresQuery.Append(oleDataBaseConnection.GetFieldById(i) + ", "); 
         break; 
       } 
      } 
     } 

     postgresQuery = postgresQuery.Remove(postgresQuery.Length - 2, 2); 
     postgresQuery.Append(") "); 

     counter++; 

     //run 100 insert statements at a time 
     if (counter % 100 == 0) 
     { 
      postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 

      postgresQuery.Clear(); 
     } 
    } 
} 
+0

あなたは事前にレコードの数を知っていればあなたは、単にデクリメントし、テストすることができます。最初のバッチに含まれるレコード数は少なく、残りは数百になります。 –

答えて

1

を我々は100で割ったカウンタの残りの値がある場合はwhileループが終了した後、すべてのレコードが挿入されていることを知っていますしたがって、カウンタの残りの値を100で割った値が0でない場合でも挿入する必要があるレコードがあるという結論を導くことができます。

だから、直接、whileループの下に、この部分を追加します。

if (counter % 100 != 0) 
{ 
    postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 

    postgresQuery.Clear(); 
} 
+0

ありがとう。それは遠くないと知っていたが、今日は本当に疲れていて、灰白質はまだ目覚めていない=)コーヒーの時間 – CSharpened

0

私はメインのロジックをチェックしていないが、それはすでにほとんどの作業(とちょうど最後の数の記録をしていない)だ場合、その後、あなただけのループの外にもう一つのINSERT文を追加する必要があります(ループの後完了しました)最後のいくつかのレコードを挿入する:

編集:ちょうどcounter % 100 == 0の場合、クエリが既にクリアされているので、counter % 100 != 0のチェックを行う必要はありません。ループ本体の外側

if (oleDataBaseConnection.HasRows()) 
{ 
    int counter = 0; 

    Spinner spinner = new Spinner(); 

    StringBuilder postgresQuery = new StringBuilder(); 

    Dictionary<string, string> postgreSQLQueries = TypeConversion.GetQueryDictionary("POSTGRESQL"); 

    while (oleDataBaseConnection.NextRecord()) 
    { 
     string postgreSQLInsertQuery; 

     postgreSQLQueries.TryGetValue("INSERT", out postgreSQLInsertQuery); 

     postgreSQLInsertQuery = postgreSQLInsertQuery.Replace("{0}", tableName); 
            spinner.Turn(); 

     postgresQuery.Append(postgreSQLInsertQuery); 

     postgresQuery.Append("("); 

     int columnCounter = 0; 

     //add a column parameter to query for each of our columns 
     foreach (KeyValuePair<string, string> t in destinationColumnData) 
     { 
      postgresQuery.Append(t.Key + ","); 
      columnCounter++; 
     } 

     postgresQuery = postgresQuery.Remove(postgresQuery.Length - 1, 1); 
     postgresQuery.Append(") "); 

     postgresQuery.Append("VALUES ("); 

     //Loop through values for column names/types 
     for (int i = 0; i < columnCounter; i++) 
     { 
      if (String.IsNullOrEmpty(oleDataBaseConnection.GetFieldById(i))) 
      { 
       postgresQuery.Append("NULL, "); 
      } 
      else 
      { 
       switch (foobar[i].ToUpper()) 
       { 
        case "TEXT": 
         postgresQuery.Append("$$" + oleDataBaseConnection.GetFieldById(i) + "$$, "); 
         break; 
        case "GEOMETRY": 
         postgresQuery.Append("ST_GeomFromText('" + oleDataBaseConnection.GetFieldById(i) + "'), "); 
         break; 
        default: 
         postgresQuery.Append(oleDataBaseConnection.GetFieldById(i) + ", "); 
         break; 
       } 
      } 
     } 

     postgresQuery = postgresQuery.Remove(postgresQuery.Length - 2, 2); 
     postgresQuery.Append(") "); 

     counter++; 

     //run 100 insert statements at a time 
     if (counter % 100 == 0) 
     { 
      postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 

      postgresQuery.Clear(); 
     } 
    } 
    // this is outside the main loop (but inside the HasRows check) 
    // Could check if stringbuilder has just been cleared. 
    if (postgresQuery.Length > 0) 
    { 
     postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 
     postgresQuery.Clear(); 
    } 
} 
+0

'counter%100!= 0'は、データベースへの不要なラウンドトリップを避けることができます(データアクセス技術が空のクエリを無視するか、DBに送信するかによって異なります) –

+0

確かに。この行はまた、データベースへの不要な移動を回避します: 'if(postgresQuery.Length> 0)' –

1

、最終的なクリーンアップを実行した:

//run 100 insert statements at a time 
    if (counter % 100 == 0) 
    { 
     postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 

     postgresQuery.Clear(); 
    } 
} 
if (counter % 100 != 0) 
{ 
    postgresSQLDBConnection.PostgreSQLExecutePureSqlNonQuery(postgresQuery.ToString()); 
} 
関連する問題