我正在插入大量数据。我认为将多个INSERT语句包装到"批处理"中是个好主意,所以我使用Transaction每千个INSERT语句向服务器发送一次:
using (var reader = selectCommand.ExecuteReader())
{
var rowCount = 0;
using (var transaction = mySqlConnection.BeginTransaction())
{
// Build the params used for insert queries
var paramCount = reader.FieldCount;
var queryParamBuilder = new StringBuilder();
for (int paramNo = 0; paramNo < paramCount; paramNo++)
{
queryParamBuilder.Append($"@p{paramNo},");
}
// Remove final comma ,
queryParamBuilder.Remove(queryParamBuilder.Length - 1, 1);
var queryParam = queryParamBuilder.ToString();
while (reader.Read())
{
using (var insertCommand = new MySqlCommand($"INSERT INTO {table.Name} VALUES({queryParam})", mySqlConnection))
{
insertCommand.Transaction = transaction;
for (int i = 0; i < paramCount; i++)
{
insertCommand.Parameters.AddWithValue($"@p{i}", reader[i]);
}
insertCommand.ExecuteNonQuery();
}
rowCount++;
}
transaction.Commit();
}
我认为Transaction会有所帮助,但是没有,我意识到每次调用insertCommand.ExecuteNonQuery()
时都会将数据发送到服务器(尽管如此,在调用Commit()之前,数据仍然没有)。原因是,如果存在PRIMARY KEY或FOREIGN KEY错误,则会在ExecuteNonQuery()
来电时立即引发异常,而不是Commit()
来电。
有没有办法只向服务器发送一次数据? TransactionScope
会为此案件工作吗?