分享
 
 
 

在ADO.NET中使用事务保护数据的完整性(5)

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

使用Savepoints

当使用savepoints, 记住仅仅是回滚savepoints并不是足够的.当局部回滚后事务必须仍被提交.同时,如果你选择实施它们,如何通知用户或处理局部完成事务是非常重要的.

接下来的代码描述创建一个新的客户,同时处理客户请求.你不得不执行慢的连接,或某些原因在检查库以前的操作库存等级成本是被禁止的.时间的99.9%整个过程应该被成功结束.然而,如果在SiteInventory表保持最小的库存等级限制,它将会失败.你可以看一下的代码:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

…public void SavepointTransaction()

{

// Create and open the connection.

SqlConnection conn = new SqlConnection();

string connString = "Server= SqlInstance;Database=Test;"

+ "Integrated Security=SSPI";

conn.ConnectionString = connString;

conn.Open();

// Create the commands.

// cmdInsertCustomer creates a new customer record

// by calling the DebitWarehouseInventory

// stored procedure.

SqlCommand cmdInsertCustomer =

new SqlCommand("CreateCustomer", conn);

cmdInsertCustomer.CommandType = CommandType.StoredProcedure;

cmdInsertCustomer.Parameters.Add

("@FirstName", SqlDbType.NVarChar, 50, "FirstName");

cmdInsertCustomer.Parameters.Add

("@LastName", SqlDbType.NVarChar, 50, "LastName");

cmdInsertCustomer.Parameters.Add

("@Email", SqlDbType.NVarChar, 50, "Email");

cmdInsertCustomer.Parameters.Add("@CID", SqlDbType.Int, 0);

cmdInsertCustomer.Parameters["@FirstName"].Direction =

ParameterDirection.Input;

cmdInsertCustomer.Parameters["@LastName"].Direction =

ParameterDirection.Input;

cmdInsertCustomer.Parameters["@Email"].Direction =

ParameterDirection.Input;

cmdInsertCustomer.Parameters["@CID"].Direction =

ParameterDirection.Output;

// cmdRequestMaterials creates a pick list

// of the materials requested by the customer

// by calling the InsertMaterialsRequest

// stored procedure.

SqlCommand cmdRequestMaterials =

new SqlCommand("InsertMaterialsRequest", conn);

cmdRequestMaterials.CommandType = CommandType.StoredProcedure;

cmdRequestMaterials.Parameters.Add

("@CustomerID", SqlDbType.Int, 0, "CustomerId");

cmdRequestMaterials.Parameters.Add

("@RequestPartID", SqlDbType.Int, 0, "PartId");

cmdRequestMaterials.Parameters.Add

("@Number", SqlDbType.Int, 0, "NumberRequested");

cmdRequestMaterials.Parameters["@CustomerID"].Direction =

ParameterDirection.Input;

cmdRequestMaterials.Parameters["@RequestPartID"].Direction =

ParameterDirection.Input;

cmdRequestMaterials.Parameters["@Number"].Direction =

ParameterDirection.Input;

// cmdUpdateSite debits the requested materials

// from the inventory of those available by calling

// the UpdateSiteInventory stored procedure.

SqlCommand cmdUpdateSite =

new SqlCommand("UpdateSiteInventory", conn);

cmdUpdateSite.CommandType = CommandType.StoredProcedure;

cmdUpdateSite.Parameters.Add

("@SiteID", SqlDbType.Int, 0, "SiteID");

cmdUpdateSite.Parameters.Add

("@SitePartID", SqlDbType.Int, 0, "PartId");

cmdUpdateSite.Parameters.Add

("@Debit", SqlDbType.Int, 0, "Debit");

cmdUpdateSite.Parameters["@SiteID"].Direction =

ParameterDirection.Input;

cmdUpdateSite.Parameters["@SitePartID"].Direction =

ParameterDirection.Input;

cmdUpdateSite.Parameters["@Debit"].Direction =

ParameterDirection.Input;

// Begin the transaction and enlist the commands.

SqlTransaction tran = conn.BeginTransaction();

cmdInsertCustomer.Transaction = tran;

cmdUpdateSite.Transaction = tran;

cmdRequestMaterials.Transaction = tran;

try

{

// Execute the commands.

cmdInsertCustomer.Parameters["@FirstName"].Value

= "Mads";

cmdInsertCustomer.Parameters["@LastName"].Value

= "Nygaard";

cmdInsertCustomer.Parameters["@Email"].Value

= "MadsN@AdventureWorks.com";

cmdInsertCustomer.ExecuteNonQuery();

tran.Save("Customer");

cmdRequestMaterials.Parameters["@CustomerID"].Value

= cmdInsertCustomer.Parameters["@CID"].Value;

cmdRequestMaterials.Parameters["@RequestPartID"].Value

= 3;

cmdRequestMaterials.Parameters["@Number"].Value

= 22;

cmdRequestMaterials.ExecuteNonQuery();

cmdUpdateSite.Parameters["@SitePartID"].Value

= 3;

cmdUpdateSite.Parameters["@Debit"].Value

= 22;

cmdUpdateSite.Parameters["@SiteID"].Value

= 4;

cmdUpdateSite.ExecuteNonQuery();

// Commit the transaction.

tran.Commit();

}

catch(SqlException sqlEx)

{

try

{

// Roll back the transaction

// to the savepoint.

Console.WriteLine(sqlEx.Message);

tran.Rollback("Customer");

tran.Commit();

// Add code to notify user or otherwise handle

// the fact that the procedure was only

// partially successful.

}

catch(SqlException ex)

{

// If the partial rollback fails,

// roll back the whole transaction.

Console.WriteLine(ex.Message);

tran.Rollback();

// Additional error handling if needed.

}

}

finally

{

// Close the connection.

conn.Close();

}

}

使用内嵌事务

内嵌事务如savepoints一样考虑同样的注意事项.为了确认局部回滚仍然需要提交,处理局部完成过程.

以下的代码示例描述创建一个新的订单,同时创建了订单项目.如果创建订单列表失败-可能是PartID无效的对于选择的位置来说-你仍然需要进入订单,在其它方面选取.内嵌的try/catch 块封装了内嵌事务用来处理选取列表,允许内部事务提交或失败依赖于外部的事务.外部的catch 块回滚整个事务如果订单创建过程失败.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.OleDb;

using System.Text;

…public void NestedTransaction(string UID, string pwd)

{

// Create and open the connection.

OleDbConnection conn = new OleDbConnection();

StringBuilder sb = new StringBuilder();

sb.Append("Jet OLEDB:System database=");

sb.Append(@"C:\Databases\system.mdw;");

sb.Append(@"Data Source=C:\Databases\orders.mdb;");

sb.Append("Provider=Microsoft.Jet.OLEDB.4.0;");

sb.Append("User ID=" + UID + ";Password=" + pwd);

string connString = sb.ToString();

conn.ConnectionString = connString;

conn.Open();

// Create the commands.

string cmdString = "Insert into Orders"

+ " (OrderID, OrderDate, CustomerID)"

+ " values('ABC60', #4/14/04#, 456)";

OleDbCommand cmdInsertOrder = new OleDbCommand(cmdString,conn);

//No need to insert OrderLineID, as that is an AutoNumber field.

cmdString = "Insert into OrderLines (OrderID, PartID, Quantity)"

+ " values('ABC60', 25, 10)";

OleDbCommand cmdInsertOrderLine =

new OleDbCommand(cmdString,conn);

cmdString = "Insert into PickList (OrderID, PartID, Quantity)"

+ " values('ABC60', 25, 10)";

OleDbCommand cmdCreatePickList =

new OleDbCommand(cmdString,conn);

// Begin the outer transaction and

// enlist the order-related commands.

OleDbTransaction tran = conn.BeginTransaction();

cmdInsertOrder.Transaction = tran;

cmdInsertOrderLine.Transaction = tran;

try

{

// Execute the commands

// to create the order and order

// line items.

cmdInsertOrder.ExecuteNonQuery();

cmdInsertOrderLine.ExecuteNonQuery();

// Create a nested transaction

// that allows the pick list

// creation to succeed or fail

// separately if necessary.

OleDbTransaction nestedTran = tran.Begin();

// Enlist the pick list command.

cmdCreatePickList.Transaction = nestedTran;

try

{

// Execute the pick list command.

cmdCreatePickList.ExecuteNonQuery();

// Commit the nested transaction.

nestedTran.Commit();

}

catch(OleDbException ex)

{

//Roll back the transaction.

nestedTran.Rollback();

// Add code to notify user or otherwise handle

// the fact that the procedure was only

// partially successful.

}

// Commit the outer transaction.

tran.Commit();

}

catch(OleDbException ex)

{

//Roll back the transaction.

tran.Rollback();

//Additional error handling if needed.

}

finally

{

// Close the connection.

conn.Close();

}

}

注:引擎提供者为Microsoft OLE DB,其支持内嵌事务.

总结

事务是一个强有力的方式,用来保证数据修改任务得到合适的处理,同时ado.net让我们处理数据存取的能力更容易.当你构建你的数据读取和使用方案的时候考虑到这些情况,你将会保证你数据的完整性.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有