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

  • 相关阅读:
    JavaScript解析顺序和变量作用域
    JS-BOM
    原生对象-Array
    JavaScript01
    css3动画
    scc的使用
    CSS3学习总结
    Js数组方法大全
    JavaScript判断变量是否为数组
    浏览器兼容性问题及解决方案
  • 原文地址:https://www.cnblogs.com/wangweixznu/p/378001.html
Copyright © 2020-2023  润新知