zoukankan      html  css  js  c++  java
  • 微软企业库 5 启用事务回滚

                SqlDatabase db = new SqlDatabase(@"server=.\sql2008;uid=sa;pwd=123456;database=DBTEST");

                
    using (DbConnection connection = db.CreateConnection())
                {
                    
    //打开链接
                    connection.Open();
                    
    //创建事务
                    DbTransaction Tran = connection.BeginTransaction();
                    
    try
                    {

                        db.ExecuteNonQuery(Tran, CommandType.Text, 
    "Insert Into TLog (DEC) Values('123')");
                        db.ExecuteNonQuery(Tran, CommandType.Text, 
    "Insert Into TLog (DEC) Values('124')");
                        db.ExecuteNonQuery(Tran, CommandType.Text, 
    "Insert Into TLog (DEC) Values(125)");
                        
    //提交事务
                        Tran.Commit();
                    }
                    
    catch (Exception Ex)
                    {
                        
    //出错回滚
                        Tran.Rollback();
                        Console.WriteLine(Ex.ToString());
                    }
                    
    finally
                    {
                        
    //关闭连接
                        connection.Close();
                    }

                    Console.WriteLine(
    "OK");
                    Console.ReadKey();
                }

    不要忘记在执行时加入“Tran”,否则不起做用的

  • 相关阅读:
    图的应用详解-数据结构
    图的遍历
    node.js基础模块http、网页分析工具cherrio实现爬虫
    NodeJS制作爬虫全过程
    Nodejs爬虫进阶教程之异步并发控制
    asp.net的临时文件夹
    Cms WebSite 编译非常慢
    查看数据库的表被谁锁住了,以及如何解锁
    WinRar 设置默认的压缩格式为zip
    Can not Stop-Computer in powershell 6.0
  • 原文地址:https://www.cnblogs.com/hantianwei/p/2117460.html
Copyright © 2011-2022 走看看