sqlDataSource的事務(以insert爲例)
事務必須手工添加(code by jiangxihua from <asp.net 2.0開發詳解inc#>)
using System.Data.Common;
public partial class sqldsTransaction:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { } protected void btInsert_Click(object sender,EventArgs e) { sqldsEmplyees.Insert(); } protected void sqldsEmployees_Inserting(object sender,SqlDataSourceCommandEventArgs e) { Dbcommand cmd=e.command; Dbconnection conn=cmd.Connection; conn.Open(); DbTransaction tran=conn.BeginTransaction(); cmd.Transaction=tran; } protected void sqldsEmployees_Inserted(object sender,SqlDataSourceCommandEventArgs e) { if(null==e.Exception) { e.Command.Transaction.Commit(); showMsg("Insert Ok,Transaction Ok!"); } else { e.Command.Transaction.Rollback(); showMsg("Insert Failer,Transaction Rollback!"); } } protected void showMsg(string AlertMessage) { Literal txtMsg=new Literal(); txtMsg.Text="<script>alert('"+AlertMessage+"')</script>"+"<br/>"; Page.Controls.Add(txtMsg); } }
|