上海网站建设高端定制网络服务公司,做网站按钮,国外做鞋子的网站,扬中网站推广服务这一节#xff0c;我要说明的是在数据集中添加事务#xff0c; 在这里说明一下事务的必要性#xff1a; 大多数基于 web 的电子邮件客户端都使用一个网格列出每条消息#xff0c;除了包含邮件的信息#xff08;主题、发送者等等#xff09;外#xff0c;还包括一个复选框… 这一节我要说明的是在数据集中添加事务 在这里说明一下事务的必要性 大多数基于 web 的电子邮件客户端都使用一个网格列出每条消息除了包含邮件的信息主题、发送者等等外还包括一个复选框。该界面允许用户删除多个消息方法是先勾选上它们再单击“Delete Selected Messages ”按钮。在用户通常编辑多条不同记录的情况下一个批量编辑界面则是一项理想的选择。用不着让用户对每条要修改的记录单击Edit 进行修改然后单击 Update 批量编辑界面里每条记录都有其各自的编辑界面。用户可以快速地修改需要更改的那几行然后单击“Update All” 按钮保存这些更改。在本系列教程中我们将探讨如何创建插入、编辑和删除批量数据的界面。 执行批操作时重要的是确定是否会出现批操作中的某些操作可能成功而其它操作可能失败的情况。考虑一个批量删除界面如果选择的第一条记录删除成功但第二条由于违反外健约束而失败时会出现什么情况第一条记录的删除操作应该回滚还是可以接受删除第一条记录 如果想把批操作作为原子操作 对待即所有的操作步骤要么都成功要么都失败则需要对数据访问层进行扩展以支持数据库事务 。数据库事务确保INSERT 、UPDATE 和 DELETE 语句集的原子数在事务的保护下执行大多数当代数据库系统都支持数据库事务特性。 事务概述 大多数数据库都支持事务它可以把多个数据库命令当成一个逻辑单元进行处理。保证组成事务的数据库命令是原子级表示所有的命令要么都失败要么都成功。 一般来说事务通过 SQL 语句来实现使用如下的模式 指示事务的开始。 执行构成事务的 SQL 语句。 如果步骤 2 中的一个语句出错则回退该事务。 如果步骤 2 中的所有语句都没有错误 则提交该事务。位于App_Code 文件夹的 DAL 子文件夹中。在 DAL 文件夹中创建一个名为 TransactionSupport 的子文件夹并在里面添加一个名为s_courseinfoTableAdapter.TransactionSupport.cs 的新的类文件。该文件将保持s_courseinfoTableAdapter的部分实现s_courseinfoTableAdapter 包含使用事务执行数据修改的方法。 在类中添加 如下代码 注意 这里使用的类是partical 部分类将数据库事务操作封装在类的内部 代码 namespace GYsmsTableAdapters{/// summary///s_courseinfoTableAdapter 的摘要说明/// /summary partial class s_courseinfoTableAdapter {private SqlTransaction _transaction;private SqlTransaction Transaction {get {return this._transaction; }set {this._transaction value; } }} 在加入 开始事务的方法 代码 public void BeginTransaction() {// Open the connection, if needed if (this.Connection.State ! ConnectionState.Open)this.Connection.Open();// Create the transaction and assign it to the Transaction property this.Transaction this.Connection.BeginTransaction();// Attach the transaction to the Adapters foreach (SqlCommand command in this.CommandCollection) { command.Transaction this.Transaction; }this.Adapter.InsertCommand.Transaction this.Transaction;this.Adapter.UpdateCommand.Transaction this.Transaction;this.Adapter.DeleteCommand.Transaction this.Transaction; } 再下来是提交和回滚事务操作 代码 public void CommitTransaction() {// Commit the transaction this.Transaction.Commit();// Close the connection this.Connection.Close(); }public void RollbackTransaction() {// Rollback the transaction this.Transaction.Rollback();// Close the connection this.Connection.Close(); } 在类中添加事务更新方法 代码 /// summary/// 事务性更新/// /summary/// param namedataTable/param/// returns/returns public int UpdateWithTransaction(GYsms.s_courseinfoDataTable dataTable) {this.BeginTransaction();try {// Perform the update on the DataTable int returnValue this.Adapter.Update(dataTable);// If we reach here, no errors, so commit the transaction this.CommitTransaction();return returnValue; }catch {// If we reach here, there was an error, so rollback the transaction this.RollbackTransaction();throw; } } 这样就完成了DAL层的事务添加了 这样在BLL层添加事务性的方法事务性添加事务性删除 代码 /// summary/// 批量事务性更新/// /summary/// param nameproducts/param/// returns/returns public int UpdateWithTransaction(GYsms.s_courseDataTable Coursetable) {return Adapter.UpdateWithTransaction(Coursetable); }/// summary/// 批量事务性删除/// /summary/// param nameproductIDs/param public void DeleteCourseWithTransaction (System.Collections.Generic.Listint CourseIDs) {// Start the transaction Adapter.BeginTransaction();try {// Delete each product specified in the list foreach (int CourseID in CourseIDs) { Adapter.Delete(CourseID); }// Commit the transaction Adapter.CommitTransaction(); }catch {// There was an error - rollback the transaction Adapter.RollbackTransaction();throw; } } 在WEB层的Course,aspx网页上有一个批量删除的方法这样写就行 代码 protected void DelSel_Click(object sender, EventArgs e) { Listint delnums new Listint();foreach (RepeaterItem Item in Repeater1.Items) { CheckBox ck (CheckBox)Item.FindControl(ckbIndex); Label lab (Label)Item.FindControl(Labelid);if (ck.Checked) {int j Convert.ToInt32(lab.Text); delnums.Add(j); } } CourseAPI.DeleteCourseWithTransaction(delnums); AlertMessage.Redirect(this, 删除成功, Course.aspx); } 我在帖上在Repeater里面的代码 代码 asp:Repeater IDRepeater1 runatserverItemTemplatetrtd classchkasp:CheckBox runatserver IDckbIndex //tdtdasp:Label IDLabelid runatserver Text%#Eval(id) %/asp:Label/tdtdasp:Label IDLabelkid runatserver Text%#Eval(kid) %/asp:Label/tdtdasp:Label IDLabelkname runatserver Text%#Eval(kname) %/asp:Label/tdtdasp:Label IDLabelteacher runatserver Text%#Eval(teacher) %/asp:Label/tdtdasp:Label IDLabelterm runatserver Text%#Eval(term) %/asp:Label/tdtddivspanimg srcimages/edt.gif alt编辑 width16 height16 /a hrefCourse_edit.aspx?actioneditid%# Eval(id) % title编辑/anbsp; nbsp;img srcimages/del2.gif alt删除 width16 height16 /a hrefcourse.aspx?actiondelid%# Eval(id) % title onclickreturn confirm(真的要删除吗?); 删除/a/span/div/ItemTemplate/asp:Repeater 这样就完成数据集事务的说明了下一节我会写上Courseadd.aspx的页面批量添加课程信息. 转载于:https://www.cnblogs.com/ScriptZhang/archive/2010/05/20/1739791.html