sqlDataSource的事務(以insert爲例)

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);
}
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章