我是多线程的新手,弄清楚什么是关键部分,什么不是关键部分,这仍然是我正在研究的一项技能。在此示例中,我在多线程环境中使用了ADO.NET代码,并且不确定是否需要在此处锁定任何内容。就像“ command.Parameters.AddRange(parms);”线。我是否需要担心线程将参数添加到另一个线程“参数集合”?
internal async Task ExecuteNonQueryAsync(string spName, SqlParameter[] parms, TransactionScopeOption tranScopeOpt = TransactionScopeOption.Required)
{
using (TransactionScope scope = new TransactionScope(tranScopeOpt, TransactionScopeAsyncFlowOption.Enabled))
{
try
{
using (var connection = new SqlConnection(_connString))
{
await connection.OpenAsync();
using (var command = new SqlCommand(spName, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(parms);
await command.ExecuteNonQueryAsync();
scope.Complete();
}
}
}
catch (Exception ex)
{
...
}
}
}