1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.SqlClient;
6using System.EnterpriseServices;
7[assembly: ApplicationName( "FrankEnterpriseServicesTransactionTest")] //在组件服务浏览器里的名称
8[assembly: Description( "Coded By Frank Xu Lei 2/14/2009")] //在组件服务浏览器里的描述信息
9[assembly: ApplicationActivation(ActivationOption.Server)] //启动类型Library在创建进程里激活,运行在客户进程,Server类型系统进程激活,
10[assembly: ApplicationAccessControl( false)] //访问设置,关闭后,用户都可以访问
11namespace EnterpriseServicesCOM
12{
13 //每个服务器组件必须继承自类ServicedComponent,ServicedComponent类继承自ContextBoundObject
14 //这样服务就可以绑定到.net环境上
15 //
16 [EventTrackingEnabled( true)] //允许监视,组件对象浏览器可以看到。默认关闭,因为会降低性能
17 [Description( "Coded By Frank Xu Lei For EnterpriseServices Transaction Test")] //组件对象浏览器显示的描述信息
18 //[JustInTimeActivation(false)]//打开或者关闭JIT启动
19 [ObjectPooling( true, 1, 10)] //是否启用对象池,如果对象创建消耗更多的资源,可以考虑对象池
20 [Transaction(TransactionOption.Supported)] //是否需要支持事务特性
21
22 public class EnterpriseServicesCOMPlusTransaction : ServicedComponent
23 {
24 //构造函数
25 public EnterpriseServicesCOMPlusTransaction()
26 {
27
28 }
29 //新增用户user信息,姓名长度表name和address的长度都是10字符,user2的长度都是100字符,此处调用Ado.net据持久层方法
30 //[AutoComplete]//设置方法属性,自动更新方法调用的结果
31 public bool AddUser( string name, string address)
32 {
33 //调用另外一个COM组件的方法,主要是测试事务的完整性
34 SqlConnection Conn;
35 try
36 {
37 //user,第1次执行的插入操作,字段长度限制10个字符
38 //使用DataSet向导创建方法操作数据库,
39 EnterpriseServicesCOM.DatabaseDataSetTableAdapters.userTableAdapter _userTableAdapter = new EnterpriseServicesCOM.DatabaseDataSetTableAdapters.userTableAdapter();
40 _userTableAdapter.Insert(name, address);
41 /** /////user2,第2次执行的插入操作,字段长度限制100个字符
42 EnterpriseServicesCOM.DatabaseDataSetTableAdapters.user2TableAdapter _user2TableAdapter = new EnterpriseServicesCOM.DatabaseDataSetTableAdapters.user2TableAdapter();
43 _user2TableAdapter.Insert(name, address);
44 //也可以自己写代码实现数据库的操作
45
46 //SqlConnection Conn = new SqlConnection("Data Source=|DataDirectory|Database.sdf");
47 //Conn.Open();
48 /** ///// //user,第1次执行的插入操作,字段长度限制10个字符
49 //SqlCommand sqlCommand = new SqlCommand("INSERT INTO [user] (name, address) VALUES (" + name + "," + address + ")");
50 //sqlCommand.ExecuteNonQuery();
51 /** /////user2,第2次执行的插入操作,字段长度限制100个字符
52 //sqlCommand.CommandText = "INSERT INTO [user2] (name, address) VALUES (" + name + "," + address + ")";
53 //sqlCommand.ExecuteNonQuery();
54
55 ContextUtil.SetComplete(); //事务成功
56 //Conn.Close();
57 return true;
58 }
59 catch (Exception e)
60 {
61 ContextUtil.SetAbort(); //失败,事务回滚,终止
62 return false;
63 throw e;
64
65 }
66 finally
67 {
68 //if (Conn != null)
69 //{
70 // Conn.Dispose();//释放资源
71 // Conn = null;
72 //}
73 }
74 }
75 //更新数据库的2个表,此处可以调用数据持久层方法
76 //[AutoComplete]//设置方法属性,自动更新方法调用的结果
77 public bool UpdateUser( string name, string address, int id)
78 {
79 //调用另外一个COM组件的方法,主要是测试事务的完整性
80 SqlConnection Conn;
81 try
82 {
83 //使用DataSet向导创建方法操作数据库,
84 EnterpriseServicesCOM.DatabaseDataSetTableAdapters.userTableAdapter _userTableAdapter = new EnterpriseServicesCOM.DatabaseDataSetTableAdapters.userTableAdapter();
85 _userTableAdapter.Update(name,address,id);
86
87 EnterpriseServicesCOM.DatabaseDataSetTableAdapters.user2TableAdapter _user2TableAdapter = new EnterpriseServicesCOM.DatabaseDataSetTableAdapters.user2TableAdapter();
88 _user2TableAdapter.Update(name, address, id);
89 //也可以自己写代码实现数据库的操作
90 //SqlConnection Conn = new SqlConnection("Data Source=|DataDirectory|Database.sdf");
91 //Conn.Open();
92 /** /////
93 //SqlCommand sqlCommand = new SqlCommand("UPDATE [user] SET name = " + name + ", address =" + address + "WHERE (id =" + id + ")");
94 //sqlCommand.ExecuteNonQuery();
95
96 //sqlCommand.CommandText = "UPDATE [user2] SET name = " + name + ", address =" + address + "WHERE (id =" + id + ")";
97 //sqlCommand.ExecuteNonQuery();
98
99 ContextUtil.SetComplete(); //事务成功
100 return true;
101 //Conn.Close();
102 }
103 catch (Exception e)
104 {
105 ContextUtil.SetAbort(); //失败,事务回滚,终止
106 return false;
107 throw e;
108 }
109 finally
110 {
111 //if (Conn != null)
112 //{
113 // Conn.Dispose();//释放资源
114 // Conn = null;
115 //}
116 }
117 }
118 }
119}