开发者

Batch operation with ADO.NET

开发者 https://www.devze.com 2023-03-31 13:25 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消