在分布式事务中登记时出错。[高分.在线形]

wqbmercury 2004-11-22 11:53:34
当我用Sql Server连接局域网内的一台装有Sql Server时出现这个错误,但是我连本机上的Sql Server时就不会出这个错,请问,是什么原因造成的~,其中用到了分布式事务

其中172.16.1.81为我本机的IP
com+的代码为:
using System;
using System.EnterpriseServices;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Reflection;
[assembly: ApplicationActivation(ActivationOption.Server)]
//描述信息
[assembly: Description("sample")]
namespace COMPlusSamples
{
//表明需要事务支持
[ Transaction(TransactionOption.Required) ]
public class TxCfgClass : ServicedComponent
{
//声明为服务器应用程序,还可以选择Library,表示为库应用程序
private static string init1 = //"workstation id=\"GMCC-MERCURY\";packet size=4096;user id=sa;data source=\"172.16.1.82\";persist security info=True;initial catalog=pubs;password=123456";
"user id=sa;password=123456;initial catalog=pubs;data source=\"172.16.1.81\"";
private static string init2 = //"workstation id=\"GMCC-MERCURY\";packet size=4096;user id=sa;data source=\"172.16.1.82\";persist security info=True;initial catalog=NorthWind;password=123456";
"user id=sa;password=123;initial catalog=NorthWind;data source=\"172.16.1.208\"";
private static string init3 = "user id=mercury;data source=siserver;password=mercury";
private static string add1 = "insert into authors(au_lname,au_fname) values('test1', 'test2')";
private static string add2 = "insert into sample values('test1',22)";
private static string add3 = "insert into t_log(log) values('123456')";
//the error sql statement
//there is not table “sample”

public TxCfgClass() {
WriteLog("TxCfgClass Create Successfully!");
//Console.WriteLine("TxCfgClass Create Successfully!");
}
private void ExecOracle(string init, string sql)
{
WriteLog("正在创建SqlConneciton,连接字符串为:"+init);
OracleConnection conn = new OracleConnection(init);
WriteLog("创建成功!");
WriteLog("正在创建SqlCommand");
OracleCommand cmd = conn.CreateCommand();
WriteLog("创建成功!");
cmd.CommandText = sql;
WriteLog("将要运行的Sql语句为:"+sql);
WriteLog("正在打开连接...");
conn.Open();
WriteLog("连接以打开");
WriteLog("正在执行Sql语句");
cmd.ExecuteNonQuery();
WriteLog("执行完成!");
conn.Close();
WriteLog("关闭数据库连接");

}

private void ExecSQL(string init, string sql)
{
WriteLog("正在创建SqlConneciton,连接字符串为:"+init);
SqlConnection conn = new SqlConnection(init);
WriteLog("创建成功!");
WriteLog("正在创建SqlCommand");
SqlCommand cmd = conn.CreateCommand();
WriteLog("创建成功!");
cmd.CommandText = sql;
WriteLog("将要运行的Sql语句为:"+sql);
WriteLog("正在打开连接...");
conn.Open();
WriteLog("连接以打开");
WriteLog("正在执行Sql语句");
cmd.ExecuteNonQuery();
WriteLog("执行完成!");
conn.Close();
WriteLog("关闭数据库连接");
}



//添加一条记录到数据库

public void Add()
{
WriteLog("Ready to Connect to Sql Server!");
try
{
//在一数据库中插入一条记录
ExecSQL(init1, add1);
WriteLog("the operation in the same database completely");
//在另外一个数据库中插入两条记录
//这次执行的是一个错误的SQL语句
ExecSQL(init2, add2);
ExecOracle(init3,add3);
WriteLog("the operation in the other database completely");
WriteLog("Record(s) added, press enter...");
}
catch(Exception e)
{
//事务回滚

ContextUtil.SetAbort();
WriteLog("Because there are some errors in the operation ,so transcation abort");
WriteLog("The error is " + e.Message);
WriteLog("abort successfully");
}

}

public void WriteLog(string str)
{
System.IO.StreamWriter sw = System.IO.File.AppendText(@"C:\log.txt");
sw.WriteLine(str);
sw.Flush();
sw.Close();
}

}

}

测试程序的代码为:
using System;

using COMPlusSamples;

using System.EnterpriseServices;



public class Client

{

public static void Main()
{

TxCfgClass cfg = new TxCfgClass();

cfg.Add();

}

}
...全文
334 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqbmercury 2005-04-30
  • 打赏
  • 举报
回复
唉我能讲什么啊,一直没有解决啊~
penny417 2005-01-20
  • 打赏
  • 举报
回复
up
sprrav 2005-01-19
  • 打赏
  • 举报
回复
这么多人在up,一点技术含量都没有。
帖子久了,解决了吧?
wqbmercury (阿笨)出来讲讲解决方法。
penny417 2005-01-19
  • 打赏
  • 举报
回复
up
tengjian1981 2005-01-07
  • 打赏
  • 举报
回复
up
guowu118 2005-01-07
  • 打赏
  • 举报
回复
up
penny417 2004-12-26
  • 打赏
  • 举报
回复
up
penny417 2004-12-24
  • 打赏
  • 举报
回复
up
wqbmercury 2004-12-08
  • 打赏
  • 举报
回复
一日一up,朋友们也要帮忙UP啊
wqbmercury 2004-12-07
  • 打赏
  • 举报
回复
up
starsong 2004-12-06
  • 打赏
  • 举报
回复
学习ing
penny417 2004-12-06
  • 打赏
  • 举报
回复
up
wqbmercury 2004-12-06
  • 打赏
  • 举报
回复
up
wangjun110 2004-12-05
  • 打赏
  • 举报
回复
up
wqbmercury 2004-11-29
  • 打赏
  • 举报
回复
UP
wqbmercury 2004-11-26
  • 打赏
  • 举报
回复
UP
wqbmercury 2004-11-25
  • 打赏
  • 举报
回复
jialiang(DayAndNight) :不行啊,我就是要用到它的两步提交啊~
wqbmercury 2004-11-24
  • 打赏
  • 举报
回复
zjcxc(邹建) :
我找到了你说的这些资料,但是并不能解决此问题,上面你所提到的解决方案,只是对win2003中安装有sql 2000发生这样的问题时才能解决,可是我装的是win2000的系统
seekg 2004-11-24
  • 打赏
  • 举报
回复
up
jialiang 2004-11-24
  • 打赏
  • 举报
回复
我记得在VB6中也曾经遇到过类似的问题,当时是暂时把事物去掉了用的。
后来改了Connection的隔离状态就可以了。
加载更多回复(16)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧