zoukankan      html  css  js  c++  java
  • C#中实现事务的实例(精华)

    public void RunSqlTransaction(string myConnString)
     {
        SqlConnection myConnection = new SqlConnection(myConnString);
        myConnection.Open();

        SqlCommand myCommand = new SqlCommand();
        SqlTransaction myTrans;

        myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");
        myCommand.Connection = myConnection;
        myCommand.Transaction = myTrans;

        try
        {
          myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
          myCommand.ExecuteNonQuery();
          myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
          myCommand.ExecuteNonQuery();
          myTrans.Commit();
          Console.WriteLine("Both records are written to database.");
        }
        catch(Exception e)
        {
          myTrans.Rollback("SampleTransaction");
          Console.WriteLine(e.ToString());
          Console.WriteLine("Neither record was written to database.");
        }
        finally
        {
          myConnection.Close();
        }
    }

  • 相关阅读:
    收藏题(小试牛刀)
    博客园及相关学习地址收录
    迭代器和生成器
    字典访问的三种方法
    函数进阶(装饰器)
    函数进阶(闭包)
    wx小程序知识点(六)
    wx小程序知识点(五)
    wx小程序知识点(四)
    wx小程序知识点(三)
  • 原文地址:https://www.cnblogs.com/wangweixznu/p/378001.html
Copyright © 2011-2022 走看看