.net分布式事务例子
2024-07-10 12:59:04
供稿:网友
using system;//引用系统命名空间
using system.data.oledb;//引用ado.net命名空间
using system.data;//引用数据空间
using system.runtime.interopservices;//为了调用guid
using system.enterpriseservices;//引用com+名命空间
using system.runtime.compilerservices;//运行时编译服务器
using system.reflection;//用些全局属性取得强名属性
using system.data.sqlclient;
[assembly: applicationname("comlibrary")]
//强名文件名和文件属性。用sn.exe生成,用法 sn -k clsstock.snk
//[assembly: assemblykeyfileattribute("clsstock.snk")]
//[assembly: assemblykeyname("clsstock.snk")]
[assembly: assemblykeyfileattribute("..//..//clsstock.snk")]
[assembly: assemblykeyname("..//..//clsstock.snk")]
namespace comlibrary
{
[transaction(transactionoption.required)]
public class dboperation:servicedcomponent
{
private sqlconnection myconnection;
private sqlcommand mycommand;
/// <summary>
/// connect database
/// </summary>
/// <param name=”connection”>database config infomation
/// formatting : datasource=..;user id=..;password=...
/// </param>
/// <returns></returns>
public void connect()
{
string connection="workstation id=yanlixin;packet size=4096;user id=sa;data source=/"yanlixin//scgl/";p" +
"ersist security info=true;initial catalog=scgl;password=scgl";
//
myconnection = new sqlconnection( connection );
myconnection.open();
mycommand = new sqlcommand();
mycommand.connection = myconnection;
return;
}
/// <summary>
/// execute one insert statement and insert one record into the table
/// </summary>
/// <param name=”connection”>database config infomation
/// formatting : datasource=..;user id=..;password=...
/// </param>
/// <returns></returns>
public int commandexcute(string commandstring)
{
int returnvalue = 0;
mycommand.commandtext = commandstring;
returnvalue = mycommand.executenonquery();
return returnvalue;
}
/// <summary>
/// commit the com+ transaction
/// </summary>
public void commit()
{
contextutil.setcomplete();
if(myconnection!=null)
myconnection.close();
}
/// <summary>
/// roolback the com+ transaction
/// </summary>
public void abort()
{
contextutil.setabort();
if(myconnection!=null)
myconnection.close();
}
}
}