您的位置:首页 > 编程语言 > ASP

[转贴]在Asp.Net中的几种事务处理的方法

2005-09-08 09:21 459 查看
//======================================================================
//方法1:直接写入到Sql脚本中
//优点:和数据库结合,运行效率高
//缺点:受到数据库的约束,如果要从sqlserver移植到其他数据库,可能要重写所有事物
//======================================================================
begin trans//开始事务
declare @orderDetailsError int , @productError ing
delete from "order details" where productId = 42
select @orderDetailsError = @@ERROR
delete from Products where productId = 42
select @productError = 0 and @productError = 0
commit trans//提交事务
else
rollback trans//回滚事务

//======================================================================
//方法2:使用Ado.net实现
//优点:不受数据库限制,
//缺点:对于一个事物要操作两个以上的数据库的跨数据库的操作的实现有一点困难。
//======================================================================
public void Exec_Jiaoyi( int customerId,string customer,int lastNum,int receiptId,string seller,string principal,DateTime bargainTime , decimal addMoney , string memo )
{
SqlConnection myConn = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["connectionString"] ) ;
myConn.Open ( ) ;
SqlTransaction myTrans = myConn.BeginTransaction ( ) ;
SqlCommand myCommand=new SqlCommand ( );
myCommand.Connection = myConn ;
myCommand.Transaction = myTrans ;//开始事务
try
{
myCommand.Parameters.Add(new SqlParameter("@m_customerId", SqlDbType.Int,0)).Value=customerId;
myCommand.Parameters.Add(new SqlParameter("@m_customer", SqlDbType.NVarChar,100)).Value=customer;
myCommand.Parameters.Add(new SqlParameter("@m_lastNum", SqlDbType.Int,0)).Value=lastNum;
myCommand.Parameters.Add(new SqlParameter("@m_receiptId", SqlDbType.Int,0)).Value=receiptId;
myCommand.Parameters.Add(new SqlParameter("@m_seller", SqlDbType.NVarChar,100)).Value=seller;
myCommand.Parameters.Add(new SqlParameter("@m_principal", SqlDbType.NVarChar,100)).Value=principal;
myCommand.Parameters.Add(new SqlParameter("@m_bargainTime", SqlDbType.DateTime,0)).Value=bargainTime;
myCommand.Parameters.Add(new SqlParameter("@m_addMoney", SqlDbType.Decimal,9)).Value=addMoney;
myCommand.Parameters.Add(new SqlParameter("@m_memo", SqlDbType.NVarChar,100)).Value=memo;
SqlParameter returnParam = myCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
returnParam.Direction = ParameterDirection.Output;

//SELECT @thisId=SCOPE_IDENTITY() FROM caiwzhk
//SqlParameter returnParam = myCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
//returnParam.Direction = ParameterDirection.Output;
//returnId = (int)myCommand.Parameters["@thisId"].Value;
myCommand.CommandText = "insert into jiaoyxx_pack ( customerId,customer,lastNum,receiptId,seller,principal,bargainTime) values (@m_customerId,@m_customer,@m_lastNum,@m_receiptId,@m_seller,@m_principal,@m_bargainTime) SELECT @thisId=SCOPE_IDENTITY() FROM jiaoyxx_pack" ;
myCommand.ExecuteNonQuery ( ) ;

int returnId = (int)myCommand.Parameters["@thisId"].Value;
myCommand.CommandText = "insert into chongzhgl_pack ( bargainId,customerId,customer,addMoney,addTime,principal,memo) values ( " + returnId.ToString() + " ,@m_customerId,@m_customer,@m_addMoney,@m_bargainTime,@m_principal,@m_memo)" ;
myCommand.ExecuteNonQuery ( ) ;

myTrans.Commit ( ) ;//提交事务
}
catch ( Exception e )
{
myTrans.Rollback ( ) ;//回滚事务
throw new Exception ( e.ToString ( ) ) ;
}
finally
{
myCommand.Dispose();
myConn.Close ( ) ;
myConn.Dispose();
}

//======================================================================
//方法3:使用COM+事务
//优点:强大的事物处理机制,不但支持跨数据库,还支持负载平横等。
//缺点:运行效率不如上面两种,部署的时候优点麻烦
//======================================================================
using System.EnterpriseServices;
using System.Runtime.CompilerServices;
using System.Reflection;

// Supply the COM+ application name.
[assembly: ApplicationName("ComPlusExample")]//这个COM+应用程序的名称
// Supply a strong-named assembly.
[assembly: AssemblyKeyFileAttribute("ComPlusExample.snk")]//一定要strong-named文件

namespace cl
{
[Transaction(TransactionOption.Required)]//表示类是要支持事务的
public class ComPlusExample : ServicedComponent
{
[AutoComplete] //表示自动提交,hello()函数没有异常就commit,有异常就rollback
public string hello()
{
return "com+成功!!!";
}
}
}

//======================================================================
//备注
//======================================================================
1.创建强名称
  在编译组件之前,您需要为此组件的程序集指定一个强名称。如果不指定,COM+ 目录将不能识别该组件,也就无法注册它。实际上,您已经通过前面使用的 AssemblyKeyFile 属性指定了强名称,现在需要使用强名称工具 (Sn.exe) 创建强名称并使 GUID 与程序集关联。
打开命令提示。
要创建强名称,请在命令提示下键入以下代码,然后按 Enter 键。
sn -k ComPlusExample.snk
将 ComPlusExample.snk 文件从硬盘驱动器的根目录(通常为 C:/)复制到项目所在文件夹的 bin 目录下。
  现在,需要编译此程序,使它能生成在 COM+ 注册此组件必需的文件。在 Visual Studio .NET 中,在 Build(生成)菜单上,单击 Build(生成)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: