2012-03-29 69 views
0

如何修改以下代碼以一次運行插入語句100,但如果總共沒有100次運行,也會運行。它也需要能夠運行最後幾個超過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

如果你知道的記錄數事先你可以簡單地遞減和測試。第一批將包含更少的記錄,其餘的將有數百個。 – 2012-03-29 07:57:48

回答

1

我們知道,當循環結束後,所有的記錄都已經被插入,如果除以100計其餘值0.因此,我們還可以得出這樣的結論,即如果計數器的餘數除以100不是0,那麼仍然需要插入記錄;

所以加這部分正下方的while循環:

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

    postgresQuery.Clear(); 
} 
+0

謝謝。我知道這並不遙遠,但我今天真的很累,灰色的東西還沒有醒來=)喝咖啡的時間 – CSharpened 2012-03-29 07:46:47

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'可以避免不必要的數據庫往返(取決於數據訪問技術是否忽略空查詢,還是將它們發送到數據庫) – 2012-03-29 07:54:59

+0

確實。這一行還避免了不必要的數據庫訪問:'if(postgresQuery.Length> 0)' – 2012-03-29 08:29:29

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()); 
} 
相關問題