积极答复者
关于c#中的异常处理问题,求教!··

问题
答案
-
如果是操作数据库可以通过DbConnection.BeginTransaction来开始事务
DbCommand的Commit可以提交事务
Rollback可以回滚
参考下面这个TransactionScope类的示例
http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope(VS.80).aspx
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~- 已标记为答案 YiChun Chen 2009年11月4日 8:20
-
你好!
.NET框架提供了这种机制,可以使用TransactionScope 类,使一段代码成为事务性的代码,具体可以参考:
http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope.aspx
周雪峰- 已标记为答案 YiChun Chen 2009年11月4日 8:20
-
全部回复
-
如果是操作数据库可以通过DbConnection.BeginTransaction来开始事务
DbCommand的Commit可以提交事务
Rollback可以回滚
参考下面这个TransactionScope类的示例
http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope(VS.80).aspx
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~- 已标记为答案 YiChun Chen 2009年11月4日 8:20
-
你好!
.NET框架提供了这种机制,可以使用TransactionScope 类,使一段代码成为事务性的代码,具体可以参考:
http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope.aspx
周雪峰- 已标记为答案 YiChun Chen 2009年11月4日 8:20
-
尝试了一下TranscationScope.但是怎么好像没有效果,是我写错了还是?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Transactions;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Ceshi ceshi = new Ceshi();
public Form1()
{
InitializeComponent();
}private void button1_Click(object sender, EventArgs e)
{
int a = 0;using (TransactionScope ts = new TransactionScope())
{
try
{ceshi.a = 3;
a = Convert.ToInt32("abc");
ts.Complete();
}
catch (Exception ex)
{
//Transaction.Current.Rollback();
MessageBox.Show(ceshi.a.ToString());
}
}
}private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(ceshi.a.ToString());
}}
public class Ceshi
{
public int a;
}
}
ceshi.a一直为3,我想要的是如果发生了异常,则ceshi.a还是原来的状态,0 -