通过ado.net实现事务处理

在数据库连接上创建事务处理对象,然后调用事务处理对象来提交事务或回滚事务。简单的代码:

private void button1_click(object sender, system.eventargs e)
  {
   sqlconnection conn = new sqlconnection("data source=192.168.2.200;uid=sa; password=;database = haierhr");
   conn.open();
   /启用事务
   sqltransaction tran = conn.begintransaction();
   sqlcommand cmd = new sqlcommand();
   cmd.connection = conn;
   cmd.transaction = tran;
   try
   {
    cmd.commandtext = "update hrrollmain set totalmember=totalmember-100 where rollid = '2005070101'";
    cmd.executenonquery();
    cmd.commandtext = "update hrrollsum set totalsummember=totalsummember+100 where rollsumid = '20050701'";
    cmd.executenonquery();
    tran.commit();
    messagebox.show("事务提交成功!");
   }
   catch(exception ex)
   {
    tran.rollback();
    messagebox.show("error!"+ex.message);
   }   
  }

try{  
  OleDbConnection   myConnection   =   new   OleDbConnection(connStr);  
  myConnection.Open();  
  OleDbTransaction   myTrans   =   myConnection.BeginTransaction();  
  //用事务执行,保证数据提交成功  
   
  OleDbCommand   myCommand   =   new   OleDbCommand();  
  myCommand.Connection   =   myConnection;  
  myCommand.Transaction   =   myTrans   ;  
   
   
   
  //删除原有此流程的相关人员配置情况  
  sql_del   =   "delete   from   case_workflow_tasklist   where   proc_id="   +   procid   +   "";  
  myCommand.CommandText   =   sql_del;  
  myCommand.ExecuteNonQuery();  
   
  //删除原有此流程的相关线配置情况  
  sql_del   =   "delete   from   case_workflow_transition   where   proc_id="   +   procid   +   "";  
  myCommand.CommandText   =   sql_del;  
  myCommand.ExecuteNonQuery();  
   
  myTrans.Commit();  
  }  
  catch(Exception   Trans_error)  
  {  
  string   strErr   =   Trans_error.Message   ;  
  myTrans.Rollback();  
  Response.Write(strErr);  
  return;  
  }  
  finally  
  {  
  myConnection.Close();  
  }   
 

 

public   void   CreateMyOleDbCommand()    
  {  
        OleDbConnection   myConnection   =   new   OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data   Source=NWIND_RW.MDB");  
        myConnection.Open();  
        OleDbTransaction   myTrans   =   myConnection.BeginTransaction();  
        string   mySelectQuery   =   "SELECT   *   FROM   Categories   ORDER   BY   CategoryID";  
        OleDbCommand   myCommand   =   new   OleDbCommand(mySelectQuery,   myConnection,myTrans);  
        myCommand.CommandTimeout   =   20;  
  }  
  -----------------------------  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值