2012-03-29 88 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()); 
}