每100次迭代运行一个函数,但也捕获最后的函数

时间:2012-03-29 07:31:34

标签: c#

如何修改以下代码以一次运行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();
         } 
     }
 }

3 个答案:

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

答案 1 :(得分:1)

我们知道,如果计数器的剩余值除以100为0,则在while循环结束后插入所有记录。因此,我们还可以得出结论:如果剩余值仍然需要插入记录计数器除以100不是0;

所以直接在while循环下面添加这个部分:

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

    postgresQuery.Clear(); 
}  

答案 2 :(得分: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();
     }
 }