【发布时间】:2012-02-25 22:48:34
【问题描述】:
是否可以在我的代码中提取代码部分并让它在多个线程中运行?
应用程序通过网络将 FoxPro 数据库中的数据复制到我们的 SQL 服务器(文件非常大,因此需要以增量方式进行批量复制...
它有效,但我想提高一点速度。
1) 通过让我标记的部分在多个线程中运行,或者作为替代方案,
2) 不循环遍历数据行中的每一列,
我选择了第二个选项...(下面更新了代码)
代码
private void BulkCopy(OleDbDataReader reader, string tableName, Table table)
{
if (Convert.ToBoolean(ConfigurationManager.AppSettings["CopyData"]))
{
Console.WriteLine(tableName + " BulkCopy Started.");
try
{
DataTable tbl = new DataTable();
foreach (Column col in table.Columns)
{
tbl.Columns.Add(col.Name, ConvertDataTypeToType(col.DataType));
}
int batch = 1;
int counter = 0;
DataRow tblRow = tbl.NewRow();
while (reader.Read())
{
counter++;
////This section changed
object[] obj = tblRow.ItemArray;
reader.GetValues(obj);
tblRow.ItemArray = obj;
////**********
tbl.LoadDataRow(tblRow.ItemArray, true);
if (counter == BulkInsertIncrement)
{
Console.WriteLine(tableName + " :: Batch >> " + batch);
counter = PerformInsert(tableName, tbl, batch);
batch++;
}
}
if (counter > 0)
{
Console.WriteLine(tableName + " :: Batch >> " + batch);
PerformInsert(tableName, tbl, counter);
}
tbl = null;
Console.WriteLine("BulkCopy Success!");
}
catch (Exception)
{
Console.WriteLine("BulkCopy Fail!");
}
finally
{
reader.Close();
reader.Dispose();
}
Console.WriteLine(tableName + " BulkCopy Ended.");
}
}
更新 我选择了第二个选项
我不知道在 while(reader.Read()) 循环中我可以执行以下操作。我没有帮助大大提高应用性能
while (reader.Read())
{
object[] obj = tblRow.ItemArray;
reader.GetValues(obj);
tblRow.ItemArray = obj;
tbl.LoadDataRow(tblRow.ItemArray, true);
}
【问题讨论】:
标签: c# multithreading datatable multicore bulkinsert