步步为营-50-事务

说明:比较常用

1 事务的四大特性:

  1.1 原子性atomicity 一个事务中包含的多个SQL语句,要么同时成功,要么同时失败.

  1.2 一致性consistency 事务必须使数据库从从一个一致性状态变成另外一个一致性状态.(银行转账)

  1.3 隔离性 isolation 各个事务执行互不干扰(锁)

  1.4 持久性 durability 对数据库中数据的改变是永久性的.

2 事务的使用

  2.1 在SQLServer中

  
select * from UserInfo
--01 事务
--01-01 开启事务(try---catch)捕获异常
BEGIN try
    begin transaction
        Update UserInfo set StuName = N'逍遥小天狼' where EmpId = 10
        Update UserInfo set StuAge = N'年龄' where EmpId = 11
    --01-02 提交事务
    commit transaction 
END try

BEGIN catch
    --01-03回滚事务
    rollback tran; 
END catch
View Code

  2.2 在C#中

  
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TransactionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 01事务

            //01 连接字符串
            string connstr = "Data Source=127.0.0.1;uid=sa;pwd=sa;Initial Catalog=DemoDB;";

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    conn.Open();
                    //02-01创建事务
                    SqlTransaction tran = conn.BeginTransaction();
                    //02-02 把事务给cmd的事务属性
                    cmd.Transaction = tran;

                    try
                    {
                        cmd.CommandText = @"Update UserInfo set StuName = N'逍遥小天狼' where EmpId = 10
                                            Update UserInfo set StuAge = 111 where EmpId = 11";
                        cmd.ExecuteNonQuery();
                        //事务提交
                        tran.Commit();
                    }
                    catch (Exception)
                    {

                        tran.Rollback();
                    }

                }

                Console.Read();
         #endregion
            }
        }
    }
}
View Code

3 事务的调用--另一种方法

  3.1 添加引用,引入命名空间using System.Transactions;

 #region 02事务
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (SqlConnection conn = new SqlConnection(connstr))
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            conn.Open();

                            cmd.CommandText = @"Update UserInfo set StuName = N'逍遥小天狼' where EmpId = 10
                                            Update UserInfo set StuAge = 111 where EmpId = 11";
                            cmd.ExecuteNonQuery();
                            Console.WriteLine("执行成功!");
                        }
                    }
                    //提交事务
                    scope.Complete();
                }
            }
            catch (Exception)
            {

                Console.WriteLine("异常");
            }
            Console.Read();

            #endregion
事务2
原文地址:https://www.cnblogs.com/YK2012/p/6818997.html