我爱Aspx >> Asp.Net >> 在C#中使用COM+实现事务控制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类中继承,这样并不会影响该类,而只是在该类中添加了两个额外的方法,这两个方法可以使代码共享变得更加容易。
Ҷƪл˵?
使用C#调用外部Ping命令获取网络..[04-21]
C#全局变量[04-21]
COM,COM+区别[04-21]
使用C#开发COM+组件[04-21]
C# 注册COM+组件步骤[04-21]
c#相关的知识[04-21]
c#教程第一讲 开始.net探索之旅[04-21]
DOTNETc#之加密解密(对称)[04-21]
Beginning C# Objects从概念到代..[03-18]
Visual C#.NET编程精粹150例[03-18]
使用C#调用外部Ping命令获取网络..[04-21]
C#全局变量[04-21]
红颜知己[04-21]
缓存 ASP.NET 页的某些部分[04-21]
COM,COM+区别[04-21]
使用C#开发COM+组件[04-21]
C# 注册COM+组件步骤[04-21]
c#相关的知识[04-21]
爱情……砂砾[04-21]
标题未定[04-21]