如何修改以下代碼以一次運行插入語句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();
}
}
}
如果你知道的記錄數事先你可以簡單地遞減和測試。第一批將包含更少的記錄,其餘的將有數百個。 – 2012-03-29 07:57:48