我在C#中將一堆行加載到MySql中。在MS Sql中,我可以將DataReader提供給SqlBulkCopy,但MySqlBulkCopy僅將其自身作爲文件批量加載的引導程序。MySql BulkCopy /從DataReader插入
因此,我目前的解決方案是在事務處理循環中使用準備好的命令。
有沒有更快的方式來完成使用DataReader源的批量加載MySql?
這是代碼。
public override void WriteToServer(IDataReader reader)
{
const string insertFormat = "insert into `{3}`.`{0}` ({1}) values ({2});";
string names = string.Join(",",
_command.Parameters.Cast<MySqlParameter>().Select(p => p.ParameterName).ToArray());
string vals = string.Join(",",
_command.Parameters.Cast<MySqlParameter>().Select(p => "?" + p.ParameterName).
ToArray());
_command.CommandText = string.Format(insertFormat, _table, names, vals, _schema);
int reportCounter = 0;
int totalRecords = 0;
bool finished = false;
using (var connection = new MySqlConnection(_source))
{
connection.Open();
_command.Connection = connection;
_command.Prepare();
while (!finished)
{
using (MySqlTransaction dbTrans = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
{
for (int i = 0; i < BatchSize; i++)
{
if (!reader.Read())
{
finished = true;
break;
}
try
{
for (int p = 0; p < _command.Parameters.Count; p++)
{
_command.Parameters[p].Value = reader.GetValue(p);
}
_command.ExecuteNonQuery();
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
reportCounter++;
totalRecords++;
if (reportCounter >= NotifyAfter)
{
reportCounter = 0;
OnSqlRowsCopied(new SqlRowsCopiedEventArgs(totalRecords));
}
}
dbTrans.Commit();
}
}
}
}
同意,我給了一個鏡頭,並遇到了一個包的大小問題。我通過Command創建了數據庫,並沒有找到用於設置最大數據包大小的DDL語句。因此,利用您的經驗,我需要以某種方式以編程方式確定'mmap()和malloc()'之間的切換點,將我的數據包大小設置爲恰好在下,並相應地準備主插入命令。聽起來對嗎?另一個可能降低功效的問題是源數據有大量的文本字段。 – 2010-04-06 23:19:25