Batch operation with ADO.NET
Do SqlTransaction's perform a batch operation without repetitive access to the database? I don't know since SqlTransaction seems to group a bunch of SQL commands tog开发者_高级运维ether and finally commit to the database. However, every command would run an ExecuteNonQuery(), which results in a database access.
public List<Task> SubmitSheet(List<Task> tList)
{
SqlConnection conn = A2.Controller.Utils.conn;
SqlTransaction submitTransaction = null;
try
{
conn.Open();
submitTransaction = conn.BeginTransaction();
foreach (Task t in tList) {
SqlCommand submitCmd = new SqlCommand("SubmitTask", conn);
submitCmd.CommandType = CommandType.StoredProcedure;
submitCmd.Transaction = submitTransaction;
submitCmd.Parameters.Add("@tid", SqlDbType.Int).Value = t.Id;
submitCmd.ExecuteNonQuery();
}
submitTransaction.Commit();
}
finally {
if (conn.State == ConnectionState.Open) conn.Close();
}
return tList;
}
Yes, as @CKoenig says it will access the database for every command. If you want to batch the commands try to use SqlDataAdapter.AddToBatch
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx.
精彩评论