在ASP.NET 2.0中操作数据之六十一:在事务里对数据库修改进行封
最后添加如下代码到Web.sitemap文件,具体的,将其添加到“Customizing the Site Map” <siteMapNode>后面: <siteMapNode title="Working with Batched Data" url="~/BatchData/Default.aspx" description="Learn how to perform batch operations as opposed to per-row operations."> <siteMapNode title="Adding Support for Transactions" url="~/BatchData/Transactions.aspx" description="See how to extend the Data Access Layer to support database transactions." /> <siteMapNode title="Batch Updating" url="~/BatchData/BatchUpdate.aspx" description="Build a batch updating interface, where each row in a GridView is editable." /> <siteMapNode title="Batch Deleting" url="~/BatchData/BatchDelete.aspx" description="Explore how to create an interface for batch deleting by adding a CheckBox to each GridView row." /> <siteMapNode title="Batch Inserting" url="~/BatchData/BatchInsert.aspx" description="Examine the steps needed to create a batch inserting interface, where multiple records can be created at the click of a button." /> </siteMapNode> 完成后,花几分钟在浏览器里登录页面,左面的菜单列出了本部分的各项
第二步:更新数据访问层以支持数据库事务 就像我们在第一章《创建一个数据访问层》探讨的一样,位于数据访问层的强类型数据集(Typed DataSet)由DataTables 和 TableAdapters构成. DataTables保存数据,而TableAdapters提供相应的方法从数据库读取数据,并根据DataTables的改动对数据库做相应的更新,等等.记得TableAdapters有2种更新数据的模式——Batch Update 和 DB-Direct.就Batch Update模式而言, TableAdapter可以传入DataSet, DataTable, 或DataRows集,遍历这些数据对要添加、修改、删除的行执行相应的InsertCommand, UpdateCommand, or DeleteCommand方法。就DB-Direct模式而言,TableAdapter传入的是那些需要进行添加、更新、删除操作的某条记录的列的值,再使用这些值执行相关的InsertCommand, UpdateCommand, 或DeleteCommand命令. TableAdapter自动生成的方法并不使用事务.默认状态下,TableAdapter执行的每一个insert, update, 或delete操作都看作是单独的、互不相干的.假定在业务逻辑层BLL里使用DB-Direct模式来向数据库添加十条记录,代码将分十次调用TableAdapter的Insert方法. 如果前5条记录添加正常,而在添加第六条记录时发生异常,前5条记录仍然保存在数据库.同样的,用Batch Update模式来操作的话,效果亦然. 在某些情况下,我们想确保在进行一系列的改动时引入原子数(atomicity).为此,我们必须手动扩展TableAdapter,通过添加一些新的方法将InsertCommand, UpdateCommand, 和DeleteCommands命令置于事务之下.在第一章《创建一个数据访问层》里,我们考察了使用部分类(partial classes)对强类型数据集(Typed DataSet)里的DataTable的函数进行扩充.该技术同样适用于TableAdapter. 强类型数据集Northwind.xsd位于App_Code文件夹的DAL子文件夹里.在DAL文件夹里再创建一个名为TransactionSupport的子文件夹,再在里面添加一个新类,名为ProductsTableAdapter.TransactionSupport.cs (见图4).该类包含ProductsTableAdapter的使用事务的方法.
在ProductsTableAdapter.TransactionSupport.cs文件里键入如下的代码: using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace NorthwindTableAdapters { public partial class ProductsTableAdapter { 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(); } } } (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |