sqlDataSource的事务(以insert为例)

来源:互联网 发布:单片机培训 编辑:程序博客网 时间:2024/05/17 23:50
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);
}
}

 
原创粉丝点击