C#中使用COM+实现事务控制

一:用VS.NET生成一个类库 。 二:添加对System.EnterpristServices的引用,具体步骤 菜单:(项目-添加引用-在.NET选项卡选择System.EnterpristServices-确定) 三:构建类 1:源程序 using System; using System.EnterpriseServices; using System.Data.SqlClient; using System.Reflection; namespace COMPlusSamples { //表明需要事务支持 [ Transaction(TransactionOption.Required) ] //声明为服务器应用程序,还可以选择Library,表示为库应用程序 [assembly: ApplicationActivation(ActivationOption.Server)] //描述信息 [assembly: Description("sample")] public class TxCfgClass : ServicedComponent { private static string init1 = "user id=sa;password=;initial catalog=pubs;data source=(local)"; private static string init2 = "user id=sa;password=;initial catalog=NorthWind;data source=(local)"; private static string add1 = "insert into authors('au_lname','au_fname') values('test1', 'test2')"; private static string add2 = "insert into sample values('test1',22)"; //the error sql statement //there is not table “sample” public TxCfgClass() {} private void ExecSQL(string init, string sql) { SqlConnection conn = new SqlConnection(init); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } //添加一条记录到数据库 public void Add() { try { //在一数据库中插入一条记录 ExecSQL(init1, add1); Console.WriteLine("the operation in the same database completely"); //在另外一个数据库中插入两条记录 //这次执行的是一个错误的SQL语句 ExecSQL(init2, add2); Console.WriteLine("the operation in the other database completely"); Console.WriteLine("Record(s) added, press enter..."); Console.Read(); } catch(Exception e) { //事务回滚 ContextUtil.SetAbort(); Console.WriteLine("Because there are some errors in the operation ,so transcation abort"); Console.WriteLine("The error is " + e.Message); Console.WriteLine("abort successfully"); Console.Read(); } } } } 2:程序说明: 添加命名空间 using System.EnterpriseServices;因为本程序使用了其中的ContextUtil类 [ Transaction(TransactionOption.Required) ] 说明DLL需要事务支持 本程序的TxCfgClass 类从ServicedComponent类中继承,这样并不会影响该类,而只是在该类中添加了两个额外的方法,这两个方法可以使代码共享变得更加容易 程序使用的sql server数据库在本机运行,init1 和 init2是两个连接数据库的连接字符串,init连接pubs数据库,inin2连接northwind数据库,这是sql2000中自带的示例数据库。add1和add2是两条sql语句,作用是分别向两个数据库的表里添加一条记录。注意:add2是一条错误的语句,因为根本没有sample表,这样,会在执行时引起异常。(这正是我们所期望的) 在执行到add2语句时,由于它是错误的,所以会引发异常,转到错误处理语句里来执行。 ContextUtil.SetAbort();该语句使所有的数据库操作回滚,这样add1语句所插入的记录也将不存在。(达到预期目标) 四:给程序添加强名(strong name) 1:创建一对密钥 用来创建密钥的工具是称为sn.exe的共享工具。通常通过命令提示运行它,该工具可执行各种任务以生成并提取密钥。我们需要用以下方式来运行sn.exe。 sn –k key.snk 其中key.snk 代表将保存密钥的文件的名称。它的名称可以是任意的,不过习惯上带有.snk后缀名。 2:签名 签名通常是在编译时进行的。签名时,用户可利用C#属性通知编译器应该使用正确的密钥文件对DLL进行签名。要做到这一点用户需要打开工程中的AssemblyInfo.cs文件并进行修改。 [assembly:AssemblyKeyFile(“..//..//key.snk”)] 注:key.snk文件和项目文件在同一个文件夹 五:编译成DLL (具体步骤) 菜单:(生成-生成) 如果一切正常,就会生成DLL文件 六:使用regsvcs.exe将Dll注册到COM+ Services里面 我们需要用以下方式运行regsvcs.exe regsvcs dll文件名 如果一切正常的话,regsvcs.exe就会把dll输入到COM+ Services中。 至此,我们已经生成并注册了这个可以由其它程序使用的类,现在,我们来写一个控制台程序来检验这个类是否正常运行 七:构建客户机 1:新建控制台应用程序项目 菜单(文件-新建-项目) 选择控制台应用程序 ,并选择 添入解决方案 ,确定 2:同上面的第二步一样,添加对System.EnterpriseServices的引用。 3:添加对自己刚才做好的类的引用。 菜单(项目-添加引用-浏览),选择刚才生成的DLL,确定 4:输入以下程序 using System; using COMPlusSamples; using System.EnterpriseServices; public class Client { public static void Main() { TxCfgClass cfg = new TxCfgClass(); cfg.Add(); } } 5:将控制台程序设置为启动项,然后编译运行,就会看到结果。 本文来自:中国建站网(www.136z.com)原文链接:http://edu.136z.com/developer/4750.html
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值