111,092
社区成员




public virtual D Update(D t)
{
D local2;
using (IDbConnection connection = this.getDbConnection())
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
D local = this.Update(t, connection);
connection.Close();
local2 = local;
}
return local2;
}
private D Update(D d, IDbConnection conn)
{
this.OnUpdating(d);
d.Meta = this.Update<E>(conn, d.Meta);
d.setUnchange();
this.OnUpdated(d);
return d;
}
TransactionOptions options = new TransactionOptions();
options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
{
try
{
foreach(类型 var in 类型集合)
{
//一些条件判断
getProxy().Update(类型);//如果更新三条记录,第一条满足条件,后面的不满足,我第一次执行这里后数据库已变了,后面两条件在前面的判断条件就通不过.第一条变掉的值也没有回滚.这样写哪里错了?谢谢
}
MessageBox.Show("保存成功!");
scope.Complete();//不是在这里再整体提交的吗?
}
catch(Exception)
{
//...
}
}
void Add(Guid id, Guid pid)
{
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
String str = "server=VARSETUP;uid=sa;pwd=sa;database=SS";
conn = new SqlConnection(str);
try
{
using (conn = new SqlConnection(str))
{
conn.Open();
String CommandText = "insert into Product (id,Name) values ('" + id + "','Hello')";
String CommandText2 = "insert into Type (id,pid,type) values ('" + Guid.NewGuid() + "','" + pid + "','dddd')";
cmd = new SqlCommand(CommandText, conn);
cmd.ExecuteNonQuery();
cmd = new SqlCommand(CommandText2, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw ex; //重新throw error
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
Guid id = Guid.NewGuid();
Guid pid = Guid.Empty;
for (int i = 0; i < 2; i++)
{
if (i == 0)
pid = id;
else
pid = Guid.NewGuid();
Add(id, pid);
}
scope.Complete();
MessageBox.Show("OK");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Add(Guid id,Guid pid)
{
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
String str = "server=(local);uid=sa;pwd=123;database=Demo";
conn = new SqlConnection(str);
try
{
using (conn = new SqlConnection(str))
{
conn.Open();
String CommandText = "insert into Product (id,Name) values ('" + id + "','Hello')";
String CommandText2 = "insert into Type (id,pid,producttype) values ('" + Guid.NewGuid() + "','" + pid + "','dddd')";
cmd = new SqlCommand(CommandText, conn);
cmd.ExecuteNonQuery();
cmd = new SqlCommand(CommandText2, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
[E2EOperation("添加", E2ETransactionScopeOption.Required)]
public virtual D Add(D t)
{
D local2;
//这里我把事务控件写到里面
using (TransactionScope scope = new TransactionScope())
{
using (IDbConnection connection = this.getDbConnection())
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
D local = this.Add(t, connection);
connection.Close();
scope.Complete();
local2 = local;
}
}
return local2;
}
public object Add(object t)
{
if (!(t is D))
{
throw new E2EException("数据格式不正确");
}
return this.Add((D) t);
}
private D Add(D d, IDbConnection conn)
{
this.OnAdding(d);//调用新增的时候可以执行一个OnAdding方法内容
d.Meta = this.Add<E>(conn, d.Meta);
d.setUnchange();
this.OnAdded(d);
return d;
}
//在客户端使用时
override OnAdding(D d)
{
//这里面执行对B表的新增,同样B表也是调用上面的Add方法,也是有事务,这样子如果A表新增成功,B表不成功就会回滚,但是我撤在外面写就想不通为什么不行,我做一个测试,看看是不是底层操作类出了问题,非常感谢天马兄及LS各位兄弟回答
}
protected virtual void OnAdding(D d)
{
}