相关实例代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
GvBind();
}
private void GvBind()
{
this.GridView1.DataSource = DbHelperSQL.Query("SELECT * FROM Items");
this.GridView1.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// 声明定义事务
using (TransactionScope ts = new TransactionScope())
{
try
{
// 事务方法体
Sub1();
Sub2();
// 提交事务并回滚
ts.Complete();
}
catch
{ }
}
this.GvBind();
}
private static void Sub2()
{
DbHelperSQL.ExecuteSql("INSERT INTO Items(Item) VALUES('aa')");
}
private static void Sub1()
{
DbHelperSQL.ExecuteSql("INSERT INTO Items(Item) VALUES(101)");
}